简体   繁体   中英

MySQL Value Returns Null After Select

if (! empty ( $fname ) && ! empty ( $lname )) {
        $query5 = "select * from " . $db_prefix . "customer_det where (fname = '" . $fname . "' and lname = '" . $lname . "')";
            $result5 = $mysqli->query ( $query5, MYSQLI_STORE_RESULT ) or die ( mysql_error () );
    } else {
        $query5 = "select * from " . $db_prefix . "customer_det where phone = '" . $phone . "'";
        $result5 = $mysqli->query ( $query5, MYSQLI_STORE_RESULT ) or die ( mysql_error () );

    }
    while ($row=$result5->fetch_assoc) {
        $uid = $row['id'];
            $visits = $row['visits'];
        }
        var_dump($uid);
        var_dump($visits);
            var_dump($result5);
            var_dump($fname);
            var_dump($lname);

I'm not sure where I'm going wrong here. I run both of those queries in the phpMyAdmin SQL query box and it returns a row set. Then when I run it in PHP, and var_dump the variables, $uid returns NULL as well as $visits. Here's the var dump:

NULL NULL object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(34) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) } string(6) "Wesley" string(6) "Snipes"

Any advice would be great.

When you're calling fetch_assoc() , you're forgetting the parentheses:

while ($row=$result5->fetch_assoc) {

Should be:

while ($row=$result5->fetch_assoc()) {

Because of this, the loop is never entered and $uid and $visits aren't being set (so they're still null ).

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