1

i recently did lots of changes to a site i was working on and went thru the entire project replacing the old mysql_ methods of interacting with the database. in this certain script im having trouble getting it to work the same.

the old code is

$checkinfo = mysql_query("SELECT * FROM `myusers` WHERE `userid` = '$uid' LIMIT 1") or die(mysql_error());

if(mysql_num_rows($checkinfo) < 1){ //log and die if user isnt in db
  die("Incident has been logged!"); }

$myinfo = mysql_fetch_assoc($checkinfo);    

and my new code is

$checkinfo = $mysqli->query("SELECT * FROM `myusers` WHERE `userid` = '$uid' LIMIT 1") or die('Error : ('. $mysqli->errno .') '. $mysqli->error);  

if($checkinfo->fetch_row() < 1){
    die("Incident has been logged!"); } 

$myinfo = $checkinfo->fetch_assoc();

now its simply not setting my array for the rest of the code... please point out my stupidity! thanks

1
  • "went thru the entire project replacing the old mysql_ methods" do yourself a favour and abstract your database access into a class, then when you need to make further changes, you only have to do it in one place Commented Feb 11, 2015 at 10:27

1 Answer 1

3

By using ->fetch_row(), it already does fed the first row. Since you explicitly set LIMIT 1, the next fetch invocation resulted into NULL.

Change it to ->num_rows instead:

$checkinfo = $mysqli->query("SELECT * FROM `myusers` WHERE `userid` = '$uid' LIMIT 1") or die('Error : ('. $mysqli->errno .') '. $mysqli->error);  

if($checkinfo->num_rows < 1){
    die("Incident has been logged!"); // change this to something more meaningful.
} 

$myinfo = $checkinfo->fetch_assoc();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.