943,681 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 34623
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 28th, 2007
0

test mysql query for no results

Expand Post »
Hey, I'm running a mysql query, and would like to run a script if the query returns no results. But what do I use to test for no results?

Something like:

[PHP]

if (!$query) {

do this

}

[/PHP]

But just not sure how to implement it. Little help?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Jan 28th, 2007
0

Re: test mysql query for no results

php Syntax (Toggle Plain Text)
  1. if(mysql_num_rows($result)==0){
  2. //do this
  3. }
Last edited by php_daemon; Jan 28th, 2007 at 6:21 pm.
Reputation Points: 13
Solved Threads: 2
Junior Poster
php_daemon is offline Offline
138 posts
since Aug 2006
Jan 30th, 2007
0

Re: test mysql query for no results

Click to Expand / Collapse  Quote originally posted by php_daemon ...
php Syntax (Toggle Plain Text)
  1. if(mysql_num_rows($result)==0){
  2. //do this
  3. }
Thanks, that was perfect.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Mar 16th, 2008
0

Re: test mysql query for no results

Click to Expand / Collapse  Quote originally posted by php_daemon ...
php Syntax (Toggle Plain Text)
  1. if(mysql_num_rows($result)==0){
  2. //do this
  3. }
OMG, you helped me so much!! I am new to php, and I was tearing my hair out. Thank you, Thankyou,! You are my hero!!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
1supergirl is offline Offline
3 posts
since Mar 2008
Mar 6th, 2009
0

Re: test mysql query for no results

Will this work for odbc_num_rows also. I tried but I get the same message no matter what
sql Syntax (Toggle Plain Text)
  1. $sql="SELECT * FROM rac_master WHERE mrnumb='$mrnumb' AND racnumb='$racnumb' AND findetdate IS NULL";
  2. $resultx=odbc_exec($conna,$sql);
  3. if (odbc_num_rows($resultx)==0) {
  4. echo"Record Has Been Finalized";
  5. }
  6. odbc_close($conna);
Last edited by peter_budo; Mar 8th, 2009 at 7:03 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamesmjr is offline Offline
4 posts
since Mar 2009
Mar 6th, 2009
0

Re: test mysql query for no results

Can't say for sure, but one way to find out would just be to test it. Echo that variable out and see what you get.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Mar 6th, 2009
0

Re: test mysql query for no results

It returns "Record Has Been Finalized", even if the query statement is met. Does it matter what data type is being used in the table
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamesmjr is offline Offline
4 posts
since Mar 2009
Mar 6th, 2009
0

Re: test mysql query for no results

Click to Expand / Collapse  Quote originally posted by jamesmjr ...
It returns "Record Has Been Finalized", even if the query statement is met. Does it matter what data type is being used in the table
Wish I could help you here. I'm a novice at this stuff. Maybe someone more experienced will chime in.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Mar 9th, 2009
0

Re: MSSQL and num_rows

Turns out the odbc_num_rows will always return -1 when using MSSQL. I changed my connecton to mssql_connect and mssql_num_rows . Every thing is working fine now.

Example:
PHP Syntax (Toggle Plain Text)
  1. $conn=mssql_connect('localhost','******','******');
  2. $msdb=mssql_select_db("DatabaseName",$conn);
  3. $sql="SELECT * FROM Table WHERE value='$variable' ";
  4. $result=mssql_query($sql,$conn);
  5. IF (mssql_num_rows($result)==0) {
  6. echo"What ever";
  7. }
  8. mssql_close($conn);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamesmjr is offline Offline
4 posts
since Mar 2009
Jan 5th, 2012
0

Finally

I was struggling with checking my empty results set before I loop with a while block until I found the above num_rows code and it worked right off. I think I spent 3 hours on this...amazing! Now I need to redirect back to the login page but this too is a struggle because I am using php header location and it's hard because I read that you are supposed to call before anything else. I'll get it though.

Thanks everyone!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
c++learner is offline Offline
27 posts
since Jul 2010
Message:
Previous Thread in PHP Forum Timeline: Help With Getting Next Results $SQL
Next Thread in PHP Forum Timeline: install curl





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC