DotDragnet
May 24, 2012, 07:19:37 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: mysqli problem  (Read 267 times)
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« on: September 23, 2011, 01:09:35 PM »

I'm a bit behind when it comes to mysqli, and so just starting to convert over to using it. I've got a script however that just doesn't want to work and either throws up a 500 error or gives the wrong result.

Code:
$sql = $mysqli->query("SELECT dateid FROM thedates WHERE bdate = '".$thedate."' AND userid = ".$userid." LIMIT 1");
if ($sql->num_rows()) :
$sql2 = $mysqli->query("UPDATE thedates SET value = '".$status."', cvalue = '' WHERE bdate = '".$thedate."' AND userid = ".$userid." LIMIT 1");
else :
$sql2 = $mysqli->query("INSERT INTO thedates (userid, bdate, daytime, value) VALUES (".$userid.", '".$thedate."', 1, '".$status."')");
endif;

I simply want to say, does a record for this date and userid exist, if so, update it, else insert it. I'm trying to get the insert part to work. Using $sql->num_rows() says it's undefined. I've also tried:

Code:
if ($mysqli->query("SELECT dateid FROM thedates WHERE bdate = '".$thedate."' AND userid = ".$userid." LIMIT 1")) :

which always gives true, so only tries to update.

Anyone know what I'm doing wrong? Sad
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #1 on: September 23, 2011, 01:40:03 PM »

Altered the code a bit and it seems to work, although still not sure why num_rows() just gave an error all of the time

Code:
$sql = $mysqli->query("SELECT COUNT(dateid) FROM thedates WHERE bdate = '".$thedate."' AND userid = ".$userid." LIMIT 1");
$count = $sql->fetch_row();
if ($count[0]) :
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 551



View Profile Awards
« Reply #2 on: September 23, 2011, 03:21:39 PM »

num_rows is a property, not a method.
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #3 on: September 23, 2011, 07:31:33 PM »

Ahhh d'oh big grin  am used to mysql_num_rows() I just didn't pay attention enough, despite reading and re-reading the PHP manual page on num_rows over and over.

Thanks Jason smile
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!