Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to set Empty Set = False

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
When you do a basic query for something and what you are looking for is not in the table queried, you get an empty set. Trying to do this with php and I'd like to be able to set it (server if possible and its not terribly dangerous) to FALSE.

If that can't be done maybe y'all can help me out with this one. I'm querying a table for some information and some of the rows WILL be NULL values. I can't use !mysql_num_rows cause that would pretty much exclude all datasets. How can I do a check on the results to see if say, there are at least say 5 rows of information. If there aren't, it kills the process right there and gives a message. If there are it continues on and finishes the page.

Thanx a bunch

Nico
 
Could you ellaborate some more on what you want to do? I'm not sure I understand what you want to do.
This should work, if what you want can't be done.
Code:
$result = mysql_query($query);
if (mysql_num_rows($result) < 5)
    die(&quot;Less than 5 rows were returned.&quot;);
else
{
    while ($row = mysql_fetch_assoc($result))
    {
        // Do whatever you want with the data
    }
}
This assumes that you set $query to the query you want to execute.
//Daniel
 
I've actually done that and everytime, no matter if there is actually a record in the database or not, it gives the die message. Can't figure out what i'm doing wrong... argh
 
Need to add something here sorry...

This is my query with the num_rows:

$sql = mysql_query(&quot;SELECT * FROM $table WHERE serialnumber= &quot; . $serialnumber . &quot; ORDER BY serialnumber&quot;);

if(mysql_num_rows($sql) > 10)
die(&quot;There are not records matching that serialnumber.&quot;);
else {

echo &quot;<p><p><table>&quot;;
while ($row = mysql_fetch_row($sql)) {stuff}
}

stuff=the html regurgitation of the query info.

now when its set with a > symbol, it will pull up records found, and if there isn't a record by the criteria, nothing shows up-- no error message etc. With a < symbol, nothing is ever regurgitated and it ALWAYS gives the error message.

basically the current way is working exactly as if I didn't even have a num_rows argument.

 
For starters, you should have quotes around the SQL values. And the > should be a <, as you want to check if there are less than a certain amount of rows, then die.
Try this script, just to see if your PHP is messed up, or if it's somethine else:

Code:
<?php
header('Content-type: text/plain');
$conn = mysql_connect(&quot;localhost&quot;, &quot;user&quot;, &quot;pass&quot;) || die(&quot;Couldn't connect.&quot;);
mysql_select_db(&quot;your database&quot;) || die(&quot;Couldn't select the database&quot;);
$result = mysql_query(&quot;SELECT * FROM yourTable WHERE serialnumber='a serial' ORDER BY serialnumber&quot;) || die(&quot;Couldn't query.&quot;);
echo &quot;That serial number has &quot; . mysql_num_rows($result) . &quot; rows\n&quot;;
?>
Change the code in the relevant places.
Also, a serial number is usually unique, but I assume that this isn't true in your case.

//Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top