[英]Populate PHP Array With Results of MySqlI Query
我正在尝试创建一个 php 数组,其中包含执行的 mysql 查询的所有字段和行。 我尝试了以下语法,但是当我执行 echo 时,什么也没有显示。
<?php
$con=mysqli_connect("server", "user", "pass", "db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM testTable LIMIT 10";
$result = mysqli_query($con,$sql);
foreach($array as $index => $value){
echo "<p>\$index: {$index}; \$value: {$value[0]}</p>";
var_export($value);
}
mysqli_free_result($result);
mysqli_close($con);
?>```
If I change the foreach loop and use $result instead of $array - it will print on screen
$index: 0; $value:
$index: 1; $value:
And I want the actual elements (or is values the right word) of the array.
好的,根据您的编辑和 Obsidian 所说的继续。 你的循环现在是正确的,你只需要现在更新你的回声。 试试这个,让我知道结果:
foreach($result as $index => $value){
echo "<p>\$index: " . $index . "; \$value: " . $value . "</p>";
var_export($value);
}
好吧,一切都好。 $value 只是一个数组。 试试这个:
foreach($result as $index => $value){
echo "<p>\$index: " . $index . "; \$value: "; print_r($value); echo "</p>";
var_export($value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.