Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through results in mysqli

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>';
like image 439
radleybobins Avatar asked Dec 18 '12 22:12

radleybobins


People also ask

What is the result of mysqli_query?

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.

What is the difference between Fetch_assoc and Fetch_array?

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.

What does Mysqli_num_rows return?

The mysqli_num_rows() function returns the number of rows in a result set.


Video Answer


1 Answers

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.

like image 146
hek2mgl Avatar answered Sep 30 '22 04:09

hek2mgl