显示数据库记录PHP
我得到一个问题,我创建的程序,这样的消息显示显示数据库记录PHP
未定义的变量:当我想上显示数据库记录html_response在
C:\xampp\htdocs\
我的网页。
这是我创建的脚本:
呼叫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;
?>
我希望任何机构可以给我的解决方案。非常感谢。
在添加到变量前,您尚未声明该变量。
使用.=
只附加到现有变量...它不声明它,然后追加。
所以你的解决方案是在添加之前声明$html_response
。
您正试图追加到变量$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;
我看你的“答案”在这里与更多的解释 - 你应该更新你的问题,不要发布一个“答案”,不回答任何问题:]
后一个问题和以前一样,你仍然试图追加到不存在的VA可变结构。只需使用此代替(注意点不存在):
$html_response = "<table>"; // define variable initialized with string
只是一个评论,但你不必复制整个代码只是添加一行 – Akintunde007
是的,我知道,改进了一下。 –
有些问题还不清楚,现在我得到了一个错误,我把这段代码$ html_response。=“
您的查询是不安全的。使用带有占位符的准备好的语句。 – mickmackusa