pthurmond Posted November 14, 2006 Share Posted November 14, 2006 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 More sharing options...
alpine Posted November 14, 2006 Share Posted November 14, 2006 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] Link to comment https://forums.phpfreaks.com/topic/27233-checking-for-0-results-in-mysql/#findComment-124515 Share on other sites More sharing options...
The Little Guy Posted November 14, 2006 Share Posted November 14, 2006 [code]<?php$query = mysql_query("SELECT * FROM table") or die(mysql_error());$row = mysql_fetch_array($query);if(!$row){ // returned no results}else{ //returned 1 or more results}?>[/code] Link to comment https://forums.phpfreaks.com/topic/27233-checking-for-0-results-in-mysql/#findComment-124521 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.