[英]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.