This question already has an answer here:
I am getting a problem with the program I created, a message like this appears
Undefined variable: html_response in
C:\xampp\htdocs\
when I want to display a database record on my web page.
This is the script I created:
call-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
", 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;
?>
I hope any body can give me solution. Thanks very much.
</div>
You have not declared the variable before adding to it.
Using .=
only appends to the existing variable... it does not declare it and then append.
So your solution would be to declare $html_response
before adding to it.
You are trying to append to variable $html_response
, but you never define it. This way you are defining it only inside the while
loop, so its not accessible outside of it.
The call $html_response .= 'something';
is basically shorthand for this:
$html_response = $html_response . 'something';
This should work:
$html_response = ''; // initialize with empty string
while ($row = mysqli_fetch_array($result)){
// ...
$html_response .= "...";
// ...
}
echo $html_response;
I see your "answer" here with more explanation - you should update your question, not post an "answer" that does not answer anything :]
The latter problem is the same as you had before, you are still trying to append to not existing variable. Just use this instead (note the dot is not there):
$html_response = "<table>"; // define variable initialized with string