Reputation: 564
I am trying to create a php array that holds all fields and rows of the mysql query that is executed. I have tried the below syntax, but when I do a echo nothing is displayed.
<?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.
Upvotes: 1
Views: 1034
Reputation: 866
Okay, following on from your edit and what Obsidian said. Your loop is now correct you just need to update your echo now. Try this and let me know the result:
foreach($result as $index => $value){
echo "<p>\$index: " . $index . "; \$value: " . $value . "</p>";
var_export($value);
}
Okay, all good. The $value is just an array. Try this instead:
foreach($result as $index => $value){
echo "<p>\$index: " . $index . "; \$value: "; print_r($value); echo "</p>";
var_export($value);
}
Upvotes: 1