Current location: Hot Scripts Forums » Programming Languages » PHP » test if DB query is false?


test if DB query is false?

Reply
  #1 (permalink)  
Old 12-29-03, 07:56 PM
mechphisto mechphisto is offline
New Member
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question test if DB query is false?

I'm trying to make an If/Else option in the case a database query comes back with nothing. But when I use the code included below, it always comes back saying there is no result.
I'm guessing it's because it's asking if there are any results to the query
after it's already finished and the array created.
How can I get it to say basically: If there are no database records fitting
the query, then do this...otherwise do this?
Thanks!!
Liam

$query_RS_con = "SELECT * FROM $conTBL WHERE $field = \"$item\"";
$RS_con = @mysql_query($query_RS_con, $connection) or die("Couldn't query:
" . mysql_error());
while ($row_RS_con = mysql_fetch_array($RS_con)) {
$procid = $row_RS_con['uid'];
$procid2 = $row_RS_con['id'];
$procpass = $row_RS_con['cont_password'];
}
if (!($row_RS_con)) {
$statusmsg = "Your Username was not found. Please go back and try
again.<br><br><A HREF='javascript:history.go(-1)'>Click Here to go back and
try again.</a><br>(You may need to click the ACCESS menu option
twice.)<br>";
} else {
if (($pass1) == ($procpass)) {
header("Location:
http://www.url.org/contact/change.php?uid=$procid$procid2");
} else {
$statusmsg = "Your Password did not match your Username. Please go back
and try again.<br><br><A HREF='javascript:history.go(-1)'>Click Here to go
back and try again.</a><br>(You may need to click the ACCESS menu option
twice.)<br>";
}
}
Reply With Quote
  #2 (permalink)  
Old 12-29-03, 08:21 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

There is a function called mysql_num_rows() , which takes resultID and returns the number of rows. So if this is zero, that means you got an empty set.

You can do something like this:

PHP Code:

if (mysql_num_rows($result) == 0) {

      
// Your statements for empty set case.
}
else {
      
// Your statements otherwise.

Hope this is what you wanted.
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 12-29-03, 08:33 PM
mechphisto mechphisto is offline
New Member
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by blaw
Hi there,
There is a function called mysql_num_rows() , which takes resultID and returns the number of rows. So if this is zero, that means you got an empty set.
--SNIP--
Yes!! That did it! Perfect! Thanks for your help; I appreciate it. =)
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM
Show query results by in a different way mdhall PHP 4 11-09-03 11:18 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM


All times are GMT -5. The time now is 07:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.