php switching to mysqli: num_rows issue

2019-04-09 09:42发布

问题:

I recently started updating some code to MySQL improved extension, and have been successful up until this point:

// old code - works
$result = mysql_query($sql);
    if(mysql_num_rows($result) == 1){
    $row = mysql_fetch_array($result);
    echo $row['data'];
    }


// new code - doesn't work
$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]"); 
    if($result->num_rows == 1) {
    $row = $result->fetch_array();
    echo $row['data'];
    }

As shown I am trying to use the object oriented style. I get no mysqli error, and vardump says no data... but there definitely is data in the db table.

回答1:

Try this:

<?php

// procedural style

$host = "host";
$user = "user";
$password = "password";
$database = "db";

$link = mysqli_connect($host, $user, $password, $database);

IF(!$link){
    echo ('unable to connect to database');
}

ELSE {
$sql = "SELECT * FROM data_table LIMIT 1";
$result = mysqli_query($link,$sql);
    if(mysqli_num_rows($result) == 1){
    $row = mysqli_fetch_array($result, MYSQLI_BOTH);
    echo $row['data'];
    }
}
mysqli_close($link);


// OOP style 

$mysqli = new mysqli($host,$user, $password, $database);
$sql = "SELECT * FROM data_table LIMIT 1";
$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]"); /* I have added the suggestion from Your Common Sence */
    if($result->num_rows == 1) {
    $row = $result->fetch_array();
    echo $row['data'];
    }

    $mysqli->close() ;

// In the OOP style if you want more than one row. Or if you query contains more rows.    

$mysqli = new mysqli($host,$user, $password, $database);
$sql = "SELECT * FROM data_table";
$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]"); /* I have added the suggestion from Your Common Sence */
    while($row = $result->fetch_array()) {
     echo $row['data']."<br>";
    }

    $mysqli->close() ;    

?>


回答2:

In PHP v 5.2 mysqli::num_rows is not set before fetching data rows from the query result:

$mysqli = new mysqli($host,$user, $password, $database);

if ($mysqli->connect_errno) {
    trigger_error(sprintf(
        'Cannot connect to database. Error %s (%s)',
        $mysqli->connect_error,
        $mysqli->connect_errno
    ));
}

$sql = "SELECT * FROM data_table";

$result = $mysqli->query($sql);

// a SELECT query will generate a mysqli_result
if ($result instanceof mysqli_result) {
    $rows = array();
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }

    $num_rows = $result->num_rows; // or just count($rows);

    $result->close();

    // do something with $rows and $num_rows
} else {
    //$result will be a boolean
}

$mysqli->close() ;    


回答3:

As it was said, you're not checking for the errors.
Run all your queries this way

$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]");

if no errors displayed and var dumps are saying no data - then the answer is simple: your query returned no data. Check query and data in the table.



标签: php mysqli