RonnieCosta50 Posted May 5, 2013 Share Posted May 5, 2013 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 https://forums.phpfreaks.com/topic/277672-php-ifmysql_num_rowsmysql_queryselect/ Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 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 https://forums.phpfreaks.com/topic/277672-php-ifmysql_num_rowsmysql_queryselect/#findComment-1428451 Share on other sites More sharing options...
RonnieCosta50 Posted May 5, 2013 Author Share Posted May 5, 2013 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 https://forums.phpfreaks.com/topic/277672-php-ifmysql_num_rowsmysql_queryselect/#findComment-1428457 Share on other sites More sharing options...
RonnieCosta50 Posted May 5, 2013 Author Share Posted May 5, 2013 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 https://forums.phpfreaks.com/topic/277672-php-ifmysql_num_rowsmysql_queryselect/#findComment-1428460 Share on other sites More sharing options...
RonnieCosta50 Posted May 5, 2013 Author Share Posted May 5, 2013 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 https://forums.phpfreaks.com/topic/277672-php-ifmysql_num_rowsmysql_queryselect/#findComment-1428462 Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 That query won't return two rows. You're still using the wrong function. Use mysql_fetch_assoc Link to comment https://forums.phpfreaks.com/topic/277672-php-ifmysql_num_rowsmysql_queryselect/#findComment-1428463 Share on other sites More sharing options...
RonnieCosta50 Posted May 5, 2013 Author Share Posted May 5, 2013 Thank you Web Mason 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 https://forums.phpfreaks.com/topic/277672-php-ifmysql_num_rowsmysql_queryselect/#findComment-1428473 Share on other sites More sharing options...
Jessica Posted May 5, 2013 Share Posted May 5, 2013 You're welcome Newbie. Please use code tags in the future. Link to comment https://forums.phpfreaks.com/topic/277672-php-ifmysql_num_rowsmysql_queryselect/#findComment-1428482 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.