Jump to content

mySQL count wher


cfgcjm

Recommended Posts

well assuming that your table and column is named right and $id has the value you expect it to, it should work just fine.  mysql_num_rows will work but you should be doing it in the query it's faster.  And you don't have to assign an alias to it, nor do you have to specify a column.

Link to comment
https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605155
Share on other sites

It would be a lot more efficient to use the built in MySQL count function

 

Does this work?

 

<?PHP
require_once "powerdb/connect.php";
session_start();
$id = $_SESSION['id'];

$sql = "SELECT COUNT(session_id) AS Total FROM carttemp WHERE session_id='$id'";
$result = mysql_query($sql);
echo mysql_result($result, 0);

?>

Link to comment
https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605158
Share on other sites

yes it's the faster way.  mysql already knows how many rows are there with count(*) and even if you specify a column, it would have to count the rows regardless, whether you do that or select * and get php to count it.  The difference is that a) it's getting "counted" twice with num_rows and b) you're making sql select everything in your table and send it back to php when you don't need all that data.  It's like if someone asks you how many pages are in a book and instead of telling them what the last page number is, you send them the whole book..

 

what I don't understand is why the count isn't working when you say the num_rows is..

Link to comment
https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605176
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.