FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


mysql_query("are there any records in 'table' WHERE ...





ocalhoun
I need to write a php script that includes a part where the database is queried to find out if there are any records matching a given SELECT query...
It just needs to find out if there are records that match or not, but outputting the number of such records would be quite acceptable, because I could write the rest of the code accordingly.

Supposing that the way to do this is like this:
$result=mysql_query("are there any records in 'table' WHERE ...)
Would I still use $row=mysql_fetch_array($result) to get the answer, and if so, I should access the array by using $row['What?']
emem
you should use php then:

Code:

<?
      $result=mysql_query(SELECT * FROM table WHERE something = '$something');
      $num=mysql_numrows($result);
      if ($num == 0) {
      echo "There were no results.";
      }
      else {
      echo "There was ".$num." results found!";
      }
?>
alalex
you could make it more accurate using LIKE:
Code:
SELECT * FROM table WHERE something LIKE '$something'

that is usually used for that kind of queries Wink
maeglin20
emem wrote:
you should use php then:

Code:

<?
      $result=mysql_query(SELECT * FROM table WHERE something = '$something');
      $num=mysql_numrows($result);
      if ($num == 0) {
      echo "There were no results.";
      }
      else {
      echo "There was ".$num." results found!";
      }
?>


Good.. Thanks..
ocalhoun
emem wrote:

$num=mysql_numrows($result);

Aha! That's what I was missing, and therefore made it so much more difficult...
Thanks!
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.