我正在尝试创建一个 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 所说的内容。你的循环现在是正确的你现在只需要更新你的 echo 。试试这个,让我知道结果:
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);
}
关于php - 用 MySqlI 查询的结果填充 PHP 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57245316/