I am confused. I looked at the fetch_assoc array example in the manual, and I understand the example code. But what would you do if your query selected * from the table and you weren't sure what the colum names were? How can you figure out what fields are coming back to you so you can then output them to your screen?
So in the example below if the query were:
$query = "SELECT * FROM City ORDER by ID DESC LIMIT 50,5";
Then how would you know what values you could use in the square brackets for the $row[] so you could access your fields on the print line? For instance, in the example they use $row["Name"] and $row["CountryCode"] which were defined in the original query (see example from manual below), but if you returned all fields how would you know which were available to you for use?
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
Hope my question makes sense. 😕
Here is the example code from the php5 manual:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>