Jump to content

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


Go to solution Solved by 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
?>

Edited by RonnieCosta50
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

  • Solution

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>";
    }
 

Edited by RonnieCosta50
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.