cfgcjm Posted August 1, 2008 Share Posted August 1, 2008 I'm having some trouble coming up with the right syntax for this what i'm trying to do is count the rows of a table where the field "session_id" is equal to a variable ex: (but wrong) "SELECT COUNT(*) FROM carttemp WHERE session_id = '$id'; Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/ Share on other sites More sharing options...
ShaunO Posted August 1, 2008 Share Posted August 1, 2008 What's the error that is generating? It looks OK to me. You may need something like SELECT COUNT(*) AS Total FROM carttemp WHERE session_id = '$id'; Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605151 Share on other sites More sharing options...
ikmyer Posted August 1, 2008 Share Posted August 1, 2008 try... $select = "SELECT COUNT(session_id) FROM carttemp WHERE session_id = $id"; Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605152 Share on other sites More sharing options...
cfgcjm Posted August 1, 2008 Author Share Posted August 1, 2008 I got it...nvm...i needed the "mysql_num_rows" function <?PHP require_once "powerdb/connect.php"; session_start(); $id = $_SESSION['id']; $sql = "SELECT * FROM carttemp WHERE session_id='$id'"; $result = mysql_query($sql); echo mysql_num_rows($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605154 Share on other sites More sharing options...
.josh Posted August 1, 2008 Share Posted August 1, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605155 Share on other sites More sharing options...
ShaunO Posted August 1, 2008 Share Posted August 1, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605158 Share on other sites More sharing options...
cfgcjm Posted August 1, 2008 Author Share Posted August 1, 2008 ShaunO: That did work. So is ShaunO's example the faster way to do it as prescribed by Crayon? Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605168 Share on other sites More sharing options...
.josh Posted August 1, 2008 Share Posted August 1, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605176 Share on other sites More sharing options...
ShaunO Posted August 1, 2008 Share Posted August 1, 2008 Crayon Violent - my guess is it would've been the final code he was using to get get the COUNT value, since the code I gave him appears to be working. Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605178 Share on other sites More sharing options...
cfgcjm Posted August 1, 2008 Author Share Posted August 1, 2008 yeah it wasn't the query that was a problem, must have been the other syntax i had it within, or so it seems Quote Link to comment https://forums.phpfreaks.com/topic/117655-mysql-count-wher/#findComment-605182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.