PHP中的数组到字符串转换和未定义变量(在HTML中使用时)
我一直在用HTML编写一段PHP代码片段,它在另一个数组中迭代来自mysql的数组数据。但是当我运行代码时,它给出了以下两个错误 未定义变量 数组到字符串的转换在。。。。。。 下面是我的代码PHP中的数组到字符串转换和未定义变量(在HTML中使用时),php,html,mysql,Php,Html,Mysql,我一直在用HTML编写一段PHP代码片段,它在另一个数组中迭代来自mysql的数组数据。但是当我运行代码时,它给出了以下两个错误 未定义变量 数组到字符串的转换在。。。。。。 下面是我的代码 $sql = "SELECT * FROM person"; $result = mysqli_query($conn, $sql); $resultDataSet = array(); if (mysqli_num_rows($result) > 0) { while ($row = mysqli
$sql = "SELECT * FROM person";
$result = mysqli_query($conn, $sql);
$resultDataSet = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
array_push($resultDataSet, $row);
}
if ($type == "HTML") {
$htmlSerialize = "
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<?php foreach($resultDataSet as $value): ?>
<tr>
<th><?php=?".$value['Name']." ?></th>
<th><?php=?".$value['Age']." ?></th>
<th><?php=?".$value['City']." ?></th>
</tr>
<?php endforeach; ?>
</table>";
echo $htmlSerialize;
}
错误报告值未定义。PHP不会解析或编译由引号-字符串包装的代码。尝试从HTML输出中分割循环
if ($type == "HTML") {
// Start by opening the table and header
$htmlSerialize = '
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>';
// loop over the results and append each row to $htmlSerialize
foreach($resultDataSet as $value):
$htmlSerialize .= '
<tr>
<th>'. $value['Name'] .'</th>
<th>'. $value['Age'] .'</th>
<th>'. $value['City'] .'</th>
</tr>';
endforeach;
// close the table
$htmlSerialize .= '</table>';
// flush results
echo $htmlSerialize;
}
$value来自何处以及它包含什么?很难说查询返回了什么。请参阅var_dump$resultDataSet的输出。可能是列名都是小写的。@magnus我是PHP的初学者。“$value”不是for循环为每个迭代分配的对象吗?1。我看不出你在任何循环中都有这样的代码。在这段代码中有一个循环,即在使用$value之前关闭的while循环。2.变量名将是您设置的名称。我建议您花些时间阅读一些基本的PHP教程和手册。@AdamAzad我更新了这个问题
if ($type == "HTML") {
// Start by opening the table and header
$htmlSerialize = '
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>';
// loop over the results and append each row to $htmlSerialize
foreach($resultDataSet as $value):
$htmlSerialize .= '
<tr>
<th>'. $value['Name'] .'</th>
<th>'. $value['Age'] .'</th>
<th>'. $value['City'] .'</th>
</tr>';
endforeach;
// close the table
$htmlSerialize .= '</table>';
// flush results
echo $htmlSerialize;
}