I am new to mysqli and having a problem looping through results with mysqli. Unfortunately, I am only getting a single result. When I put the query into phpMyAdmin, it comes up with three results. I believe the relevant code is here and that I am just calling it wrong:
$connection = new mysqli($host, $databaseUsername, $databasePassword, $database);
if ($connection->connect_errno > 0) {
die ('Unable to connect to database [' . $connection->connect_error . ']');
}
$sql = "SELECT clientId, studentFirstName, studentLastName
FROM clients
WHERE (studentEmail = '$postEmail') OR (parentEmail = '$postEmail');";
if (!$result = $connection->query($sql)) {
die ('There was an error running query[' . $connection->error . ']');
}
echo '<select class = "toolbarDropdown" id = "toolbarDropdown-MultipleAccounts">';
while ($row = $result->fetch_array()) {
echo '<option value="'.$row["clientId"].'">'.$row["studentFirstName"].' '.$row["studentLastName"].'</option>';
}
echo '</select>';
Return value: Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
fetch_array returns value with indexing. But Fetch_assoc just returns the the value. here array location 0 contains 11 also this location name is 'id'. means just returns the value.
The mysqli_num_rows() function returns the number of rows in a result set.
You are missing the closing " at option="value <-- in your HTML
Note that
$row = $result->fetch_array()
can be replaced by
$row = $result->fetch_assoc()
Doing so, the array for each record you fetch would take half of the size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With