Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Why does this throw an error

Codes here !

Moderators: macek, egami, gesf

Why does this throw an error

Postby billygoatkaraoke » Tue Jul 24, 2012 12:05 am

Gidday

I'm using AJAX to call some php, which returns display data:

AJAX:
Code: Select all
var env_url = "page.php";   
   
   $.post(env_url,{ task: "taskVar"} ,  function(data) {
             
      $("#env").html(data);
               
   });      


PHP excerpt:
Code: Select all
$my_id = 'testID';
$sql_pm_check = mysql_query("select f.id, f.thread_id, f.to_id, f.from_id, f.time_sent, f.message, f.opened, f.recipientDelete, f.senderDelete
from (
   select thread_id, max(id) as maxid
   from mytable WHERE (to_id = $my_id OR from_id = $my_id) group by thread_id
) as x inner join mytable as f on f.thread_id = x.thread_id and f.id = x.maxid");

   $num_new_pm = mysql_num_rows($sql_pm_check);
   
   if ($num_new_pm > 0)
   {         
      echo $num_new_pm;
      
   }



It ends up echoing the error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in page.php on line 44

I use the same query on another page, and it works fine, so the query is sound. And if I substitute it for the simple query:

$sql_pm_check = mysql_query("select id from mytable WHERE thread_id = '233'");

...it successfully echos $num_new_pm, so the AJAX/php connection seems fine.

Is there something obvious in there that's tripping me up?

Thanks for your time and help.
billygoatkaraoke
New php-forum User
New php-forum User
 
Posts: 91
Joined: Sat Aug 13, 2011 7:20 pm

Re: Why does this throw an error

Postby Nullsig » Tue Jul 24, 2012 4:24 am

It's because you failed to trap your error. The query you are executing is not succeeding and the value "FALSE" is being returned which is a boolean and not a result set.

You should do one of the following:

1. Use the "or die" key words to check for the error.
Code: Select all
$sql_pm_check = mysql_query("select id from mytable WHERE thread_id = '233'") or die("You fail at queries");


Pros: quick and easy
Cons: Not very graceful. The entire script dies and a white screen with only the error message is displayed.



2. Use conditionals:
Code: Select all
if($sql_pm_check = mysql_query("select id from mytable WHERE thread_id = '233'")){
      //Do stuff on the successfully created result set
}else{
     echo "Your query failed, and you should feel bad.";
}



Pros: Allows for graceful recovery from errors. You can save the error message to a variable and echo it out in a more user friendly manner

Cons: A little more coding
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
 
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Why does this throw an error

Postby billygoatkaraoke » Tue Jul 24, 2012 5:32 am

Thanks Nullsig

The mysql error is telling me there is a syntax error - I didn't have single quotes around $my_id. What tripped me up is that this query works fine in another php page. Not sure why. But problem solved.

"select f.id, f.thread_id, f.to_id, f.from_id, f.time_sent, f.message, f.opened, f.recipientDelete, f.senderDelete
from (
select thread_id, max(id) as maxid
from mytable WHERE (to_id = $my_id OR from_id = $my_id) group by thread_id
) as x inner join mytable as f on f.thread_id = x.thread_id and f.id = x.maxid"


Thanks again!
billygoatkaraoke
New php-forum User
New php-forum User
 
Posts: 91
Joined: Sat Aug 13, 2011 7:20 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron