显示数据库记录PHP

时间:2017-10-28 09:55:23

标签: php concatenation

我遇到了我创建的程序的问题,出现了这样的消息

  

未定义的变量:C:\xampp\htdocs\

中的html_response

当我想在我的网页上显示数据库记录时。

这是我创建的脚本:

拨打-data.php

<?php
    $mysqli= mysqli_connect('localhost','sman10bdl','xx','x10x');
    $kls = $_POST['kelas'];
    $query = "SELECT * FROM data_siswa WHERE kls='$kls'";
     // Execute Query
    $result = mysqli_query($mysqli,$query);
    if (!$result) {
        printf("Error: %s\n", mysqli_error($mysqli));
        exit();
    }
    $noUrut = 0;
    while($row = mysqli_fetch_array($result)){
        $noUrut++;
        $nis = $row["nis"];
        $nisn = $row["nisn"];
        $nama = $row["nama"];
        $kls = $row["kls"];
        $sex = $row["sex"];
        $alamat = $row["alamat"];
        $telp = $row["hanphone"];

        $html_response.= "<tr>
            <td align='center'>$noUrut</td>
            <td align='center'>$nis</td>
            <td align='center'>$nisn</td>
            <td align='left'>$nama</td>
            <td align='center'>$sex</td>
            <td align='center'>$alamat</td>
            <td align='center'>$telp</td>
        </tr>";
    }
    echo $html_response;
?>

我希望任何人都可以给我解决方案。非常感谢。

2 个答案:

答案 0 :(得分:1)

您正尝试附加到变量$html_response,但您从未定义它。这样,您只能在while循环内定义它,因此无法在其外部访问它。

电话$html_response .= 'something';基本上是简写:

$html_response = $html_response . 'something';

这应该有效:

$html_response = ''; // initialize with empty string

while ($row = mysqli_fetch_array($result)){
    // ...
    $html_response .= "...";
    // ...
}

echo $html_response;

我看到了你的答案&#34;这里有更多解释 - 您应该更新您的问题,而不是发布&#34;回答&#34;什么都没有回答:]

后一个问题和以前一样,你仍然试图追加到现有的变量。只需使用它(注意点不在那里):

$html_response = "<table>"; // define variable initialized with string

答案 1 :(得分:-1)

在添加变量之前,您尚未声明变量。

使用.=仅附加到现有变量...它不会声明它然后追加。

因此,您的解决方案是在添加之前声明$html_response