php - 使用 PHP 数组输出 MySQL 查询 - foreach 循环错误“非法偏移”和“无效参数”
问题描述
为了尝试使这更容易理解,我减少了这个示例而不是我正在使用的实际代码,但我知道问题出在哪里,我只是不确定如何使用foreach
来输出我的 MySQL 查询的结果。
我有一个类似的数组
$thetablestructure = array(
array('tableheader' => 'header 1', 'tablevalue' => 'value1'),
array('tableheader' => 'header 2', 'tablevalue' => 'value2'),
);
我希望 HTML 输出是这样的:
<table>
<tr>
<th>header 1</th>
<th>header 2</tth>
</tr>
<tr>
<td>value 1</td>
<td>value 2</td>
</tr>
</table>
这是我尝试使用的代码,其中错误表示非法字符串偏移和非法值:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$keywords = $_REQUEST['keywords'];
$sql = "SELECT * FROM `stock` WHERE `model` LIKE '%$keywords%'";
$result = $conn->query($sql);
$thetablestructure = array(
array('tableheader' => 'Header 1', 'tablevalue' => 'value1'),
array('tableheader' => 'Header 2', 'tablevalue' => 'value2'),
);
echo "<table><tr>";
foreach ($thetablestructure as $thetablestructure) {
echo "<th>".$thetablestructure[tableheader]."</td>";
}
echo "</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo('<tr>');
foreach ($thetablestructure as $thetablestructure) {
echo "<td>".$row["$thetablestructure[tablevalue]."]."</td>";
}
echo('</tr>');
}
echo "</table>";
$conn->close();
最初这是我使用的代码,在尝试简化和浓缩之前:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve keyword for MySQL query and search database
$keywords = $_REQUEST['keywords'];
$sql = "SELECT * FROM `stock` WHERE `model` LIKE '%$keywords%'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "
<table>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo('<tr>');
echo "<td>".$row["$thetablestructure[tableheader]"]."</td>";
echo "<td>".$row["value2"]."</td>";
echo('</tr>');
}
echo "</table>";
} else {
echo "<p>0 results for ".$keywords."
}
$conn->close();
希望这是有道理的,有人可以帮助我。
解决方案
你犯了以下错误,key tableheader是一个字符串,所以你必须使用“'”,另外,你在foreach中使用了相同名称的数组'$thetablestructure'
foreach ($thetablestructure as $v) {
echo "<th>{$v['tableheader']}</td>";
}
接下来对于每一个你使用字符串作为变量,以及其他错误,我建议你先隔离在一个变量中,比如$v,然后再尝试使用。
while($row = $result->fetch_assoc()) {
echo('<tr>');
foreach ($thetablestructure as $t) {
$v =$t['tablevalue'];
echo "<td>".$row[$v]."</td>";
}
echo('</tr>');
}
只是观察一下,如果您在代码中专门调用“value1”和“value2”,有时维护起来更好,同时易于理解,而不是使用像 $thetablestructure 这样的结构来动态构建您想要的任何东西。
推荐阅读
- python - 即使用户不在 Discord Python 中的服务器内,如何从 Discord 用户 ID/名称获取信息
- flutter - 有一种方法可以了解 `flutter run` 何时完成安装应用程序?
- python-3.x - 如何根据 Google Map Screenshot 中的像素坐标提取每条道路并将它们放入不同的列表中?
- php - ACF 画廊图片网址
- reactjs - 为什么新的 gatsby 安装返回模块未找到错误?
- python - 在 Python 中使用“list()”和“[]”创建列表有什么区别?
- javascript - Electron.js webview 静默打印
- twitter - 如何在 twitter 和 discord webhook 之间进行最快的连接?
- android - 在导入 osmdroid 时需要帮助(AndroidManifest.xml 错误消息)
- c - 如何在双向链表中正确使用方向开关?