bnovak Posted June 7, 2010 Share Posted June 7, 2010 Yep, another count question. I have the mySQL part working correctly, but I can't figure out what I'm doing wrong in the php to echo the output. I'm counting all the rows in the table, so I'm not sure why I would need to define an array for the output? <tags> $output= mysql_query(select count(*) from email_subscriptions); echo "Join"; echo "". $output; echo ""."other users on our mailing list"; <?close tags> I'd like the output to be "Join xxx other users on our mailing list" but I'm getting "Join ResourceID2 on our mailing list" Everything points to an array error, but again I'm not outputting individual rows, I just want to return the overall count of the table. Sorry if this seems really trivial. Quote Link to comment https://forums.phpfreaks.com/topic/204133-outputting-a-count-string-inside-a-sentence/ Share on other sites More sharing options...
F1Fan Posted June 7, 2010 Share Posted June 7, 2010 $output= mysql_query(select count(*) from email_subscriptions); $row = mysql_fetch_row($output); echo "Join " . $row[0] . " other users on our mailing list"; Quote Link to comment https://forums.phpfreaks.com/topic/204133-outputting-a-count-string-inside-a-sentence/#findComment-1069172 Share on other sites More sharing options...
Psycho Posted June 7, 2010 Share Posted June 7, 2010 You are assigning the RESULT of the query to the variable $output. So, the variable is simply pointing to that result. You need to extract the record(s) from that result as shown by F1Fan. Although, here was my method: $result= mysql_query("SELECT COUNT(*) FROM email_subscriptions"); $count = mysql_result($result, 0); echo "Join {$count} other users on our mailing list"; Quote Link to comment https://forums.phpfreaks.com/topic/204133-outputting-a-count-string-inside-a-sentence/#findComment-1069173 Share on other sites More sharing options...
F1Fan Posted June 7, 2010 Share Posted June 7, 2010 I need to do more teaching, and less doing! Quote Link to comment https://forums.phpfreaks.com/topic/204133-outputting-a-count-string-inside-a-sentence/#findComment-1069174 Share on other sites More sharing options...
bnovak Posted June 7, 2010 Author Share Posted June 7, 2010 Hey whaddya know - that solved the issue! Yeah that makes sense the way you guys explained it. Thanks for the help - it would be nice if some of the documentation was as simple as this to read. Quote Link to comment https://forums.phpfreaks.com/topic/204133-outputting-a-count-string-inside-a-sentence/#findComment-1069177 Share on other sites More sharing options...
Psycho Posted June 7, 2010 Share Posted June 7, 2010 Which documentation are you referring to? I think the PHP documentation is excellent. For the mysql_query() function the documentation states in part (emphasis added): Return Values For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data. The problem with the documentation is that many people don't read all of it (myself included). We read enough to understand the basic usage and then plod ahead from there falling into the same traps that the documentation warned us against. Quote Link to comment https://forums.phpfreaks.com/topic/204133-outputting-a-count-string-inside-a-sentence/#findComment-1069185 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.