Jump to content

Checking for 0 results in MySQL


pthurmond

Recommended Posts

For my username signup script I need to check to see if the username exists. To do that I just want to run a query on the username table that tries to match the username input from the user. The basic concept is if $results > 0 then the username is taken. My concern is that it will always give errors with zero results.

Currently I use the following to pull results:
$result = mysql_query($idQuery) or die('Query failed: ' . mysql_error());

If I change it to the following will I be able to run the query error free when getting the results:
$result = mysql_query($idQuery);


Thanks,
Patrick
Link to comment
https://forums.phpfreaks.com/topic/27233-checking-for-0-results-in-mysql/
Share on other sites

Use mysql_num_rows() to check this, the query will always return 1 (true) if it is a valid query - regardless of what is found or not based on the query:

[code]
<?php

$query = mysql_query("select what from table") or die(mysql_error());

if(mysql_num_rows($query) > 0)
{
  // returned 1 or more results
}

?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.