Jump to content

trying to convert mysql_results to mysqli.


jason310771

Recommended Posts

I have been for a few days not converting one of my sites so it uses mysqli from mysql. I have managed with the little help of google that i did find change most of the mysql to mysqli but now got a problem with mysql_results.

 

this is my original code...

 

 

$res = db_query($mysqli, $sql);

if(@$res->num_rows && @mysql_result($res, 0 ,'verified') == "Yes" && @mysql_result($res, 0 ,'suspended') == "No") {

if(@mysql_result($res, 0 ,'changeofpasswordcode') != "") {

$randomcode = @mysql_result($res, 0 ,'changeofpasswordcode');

} else { $randomcode = createPasswordResetCode();

}

// do something

}

 

 

but when i changed it to what i thought it should be i am getting errors...

$res = db_query($mysqli, $sql);
    if($res->num_rows &&
    $res->fetch_object()->verified == "Yes" &&
    $res->fetch_object()->suspended == "No") {
			    if($res->fetch_object()->changeofpasswordcode != "") {
			    $randomcode = $res->fetch_object()->changeofpasswordcode;
			    } else { $randomcode = createPasswordResetCode();
					    }
    // do something
    }

the errors are...

 

Trying to get property of non-object

 

on this line...

$res->fetch_object()->suspended == "No") {

 

 

can anyone see what i might be doing wrong to getting the data from the single row from the database ?

Link to comment
Share on other sites

Every time you call fetch_object() you're fetching a new row from the results.

 

Fetch one row/object separately and reference that.

$res = db_query($mysqli, $sql);
$obj = $res->fetch_object();
                if($res->num_rows &&
                $obj->verified == "Yes" &&
                $obj->suspended == "No") {
                                        if($obj->changeofpasswordcode != "") {
                                        $randomcode = $obj->changeofpasswordcode;
                                        } else { $randomcode = createPasswordResetCode();
                                                        }
                // do something
                }

Link to comment
Share on other sites

I see, but what if there are no results in $res, would...

 

$obj = $res->fetch_object();

 

 

kick up an error ?

 

Every time you call fetch_object() you're fetching a new row from the results.

 

Fetch one row/object separately and reference that.

$res = db_query($mysqli, $sql);
$obj = $res->fetch_object();
               if($res->num_rows &&
               $obj->verified == "Yes" &&
               $obj->suspended == "No") {
                                       if($obj->changeofpasswordcode != "") {
                                       $randomcode = $obj->changeofpasswordcode;
                                       } else { $randomcode = createPasswordResetCode();
                                                       }
               // do something
               }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.