Jump to content

php if(mysql_num_rows(mysql_query("$SELECT")


RonnieCosta50

Recommended Posts

I want to count how many rows there are with the values 127 and 555:555 in certain columns.  In the table I am using, there are 2 rows with that criteria.  I'm having trouble with the IF statement I think.  Anyone know what's wrong?  I've been trying for 3 hours.  What I want it to do is tell me if there is a row or now.  That's all.

 

<?php

$query = mysql_query("SELECT COUNT(*) FROM $table WHERE $column3 = '127' AND $column5 = '555:555'")// Selects 2 rows from the database table.
$numRows = mysql_num_rows($query);
if ($numRows>0   
    {
    echo"Row exists!";
    }    
else

    {

    echo"Row does not exist";

    }
echo"</br>TEST COMPLETE";
echo"Total Rows:  $numRows";
?>

 

Link to comment
Share on other sites

A query with a COUNT will always return exactly one row (assuming no grouping or other aggregates). The count of rows matching the criteria. Run the query in mysql and you'll see. You need to give the count an alias so you can use it in your script.

Link to comment
Share on other sites

Yes you are right.  If you type it directly into sql then it will return only 1 value.  The value will be the number of rows that meet the criteria.  But when you do it in php it will return a resource ID rather than the number.  I'm trying to bypass it with mysql_num_rows.

Link to comment
Share on other sites

Sorry I think I understand what you ment now but I'm not fully getting it.  Here's what I came up with but it still not working.

<?php

$query = mysql_query("SELECT COUNT(*) AS count FROM $table WHERE $column3 = '127' AND $column5 = '555:555'")
$numRowsReferenceID = mysql_num_rows($query);

$numRows = $numRowsReferenceID['count'];
if ($numRows>0   
    {
    echo"Row exists!";
    }    
else

    {

    echo"Row does not exist";

    }
echo"</br>TEST COMPLETE";
echo"Total Rows:  $numRows" // Says there is 2 rows
?>

Link to comment
Share on other sites

Sorry I think I understand what you ment now but I'm not fully getting it.  Here's what I came up with but it still not working.

<?php

$query = mysql_query("SELECT COUNT(*) AS count FROM $table WHERE $column3 = '127' AND $column5 = '555:555'")

$numRowsReferenceID = mysql_num_rows($query);

$numRows = $numRowsReferenceID['count'];

if ($numRows>0   

    {

    echo"Row exists!";

    }    

else

    {

    echo"Row does not exist";

    }

echo"</br>TEST COMPLETE";

echo"Total Rows:  $numRows" // Says there is 2 rows

?>

Forgot to mention what the problem was with this one.  It is not entering the IF statement I think.  $numRows says there is 2 rows.  So if (2>0) should work no?

Link to comment
Share on other sites

Thank you Web Mason :thumb-up: for your help.  Got it working.

SOLUTION

$query = mysql_query("SELECT COUNT(*) AS count FROM $table WHERE $column3 = '127' AND $column5 = '555:555'");
$fetch = mysql_fetch_assoc($query);
$numberOfRows = $fetch['count'];
if ($numberOfRows>0)    
    {
    echo"In IF: $numberOfRows </br>";
    }    
else

    {
    echo"In else: $numberOfRows </br>";
    }
 

Link to comment
Share on other sites

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.