简体   繁体   中英

Is $query->get_result() the Correct Way to Get Results?

I currently have the following code;

$sql = 'SELECT * FROM customers';
$query = $con->prepare( $sql );
$query->execute();
$rr = $query->get_result();


while ($s = $rr->fetch_assoc()) {
    echo '<tr>';
    echo '<td>' . $s['name'] . '</td>';
    echo '<td>' . $s['email'] . '</td>';
    echo '<td>' . $s['mobile'] . '</td>';
    echo '</tr>';
}

Is this the correct way to get results from the database using mysqli?

It's a perfectly acceptable way of getting the results from a prepared statement, but it will only work if you have the native mysqli extension installed (if you have the native PHP installation, then it should be present). You could also use $query->bind_result() and fill variables directly, like this;

$sql = 'SELECT name,email,mobile FROM customers';
$query = $con->prepare( $sql );
$query->execute();
$query->bind_result($name, $email, $mobile);

while ($query->fetch()) {
    echo '<tr>';
    echo '<td>' . $name . '</td>';
    echo '<td>' . $email . '</td>';
    echo '<td>' . $mobile . '</td>';
    echo '</tr>';
}

Note the variables are filled (in the order they are retrieved from the database, hence why I explicitly named the columns in my example) with the data for the next line each time $query->fetch() is called, so you need the while loop to go through all the data.

Hope this helps.

<?php

//First make a connection to the database server:
$mysql = new mysqli("host_or_ip", "username", "password", "database");

//Then check your new connection
if($mysql->connect_errno){
    die("DB connection failed: ", $mysql->connect_error);
}

//Run a select query and check there are no errors
$result = $mysql->query("SELECT book_name FROM bookshelf LIMIT 30");
if(!result){
    die("Error running query: ".$result->error);
}

//Output the results
echo("".
    "<table>".
        "<thead>".
            "<tr>".
                "<th>Book Name</th>".
                "<th>Author</th>".
                "<th>Publisher</th>".
            "</tr>".
        "</thead>".
        "<tbody>");

while($row = $result->fetch_assoc()){
    echo '<tr>';
    echo '<td>'.$row['book_name'].'</td>';
    echo '<td>'.$row['author'].'</td>';
    echo '<td>'.$row['publisher'].'</td>';
    echo '</tr>';
}

echo("</tbody></table>");

I dnt know if you did a conenction to db . Please check this example too:

<?php
    $mysqli = new mysqli($dbHost, $dbUsername, $dbPassword, $dbDatabase);
    $stmt = $mysqli->prepare('select * from foobar');
    $stmt->execute();
    $stmt->store_result();
    $meta = $stmt->result_metadata();

    while ($column = $meta->fetch_field()) {
       $bindVarsArray[] = &$results[$column->name];
    }       
    call_user_func_array(array($stmt, 'bind_result'), $bindVarsArray);

    $stmt->fetch();

    echo var_dump($results);
    // outputs:
    //
    // array(3) {
    //  ["id"]=>
    //  &int(1)
    //  ["foo"]=>
    //  &string(11) "This is Foo"
    //  ["bar"]=>
    //  &string(11) "This is Bar"
    // }
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM