Having some difficulty echoing the results of a query using PHP. I'm referencing this answer to build my script. I believe I have everything copied (minus var names and the query) but I'm not getting the right result. When I query the database within MySQL Workbench via select name from restaurant
, it returns the 4 restaurant names in the table as expected. My guess is that I'm not referencing the results correctly as the query does return 4 results.
PHP CODE:
// connect
$conn = mysqli_connect($hostname, $q_user, $q_pass, $db); // these vars are initialized prior
// set params and query
$query = "select ? from restaurant";
$p1 = "name";
if($sql = $conn->prepare($query)) {
// bind and execute
$sql->bind_param("s", $p1);
$sql->execute();
$sql->store_result();
$num_results = $sql->num_rows;
echo "{$num_results} results<br>";
$sql->bind_result($r_name);
// populate page with all results
while($sql->fetch()){
echo "${r_name}<br>";
}
// free results and close query
$sql->free_result();
$sql->close();
}
//disconnet
mysqli_close($conn);
RESULT (body only)
<body>
4 reults
<br>
name
<br>
name
<br>
name
<br>
name
<br>
</body>