redarrow Posted March 3, 2006 Share Posted March 3, 2006 Hi there can you help please i want to count the number of messages a user has and echo that number cheers example thank you.Example but needs help.[code]<?php $res = mysql_query("select * from messages WHERE id=$id") $row = mysql_fetch_array($res); echo count($row); echo $row[0]; echo count($row); ?> [/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 3, 2006 Author Share Posted March 3, 2006 I was using this code the member has only got 1 message but i receve 18 please help thank you.[code]<?php $sql_host = 'xxxx'; //add host information here (localhost, mysql.host.com, etc.)$sql_un = 'xxxx'; //add your user name here$sql_pass = 'xxxx'; //add your password here$sql_db = 'xxxx'; //add your database name heremysql_connect("$sql_host", "$sql_un", "$sql_pass") or die ("Could not connect to database");mysql_select_db($sql_db) or die ("Could not select database");$res = mysql_query("select * from membercomments WHERE id='$id'"); $row = mysql_fetch_array($res); echo count($row); ?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 3, 2006 Share Posted March 3, 2006 There are at least two ways to do this...[ol type=\'1\'][*][code]<?php$q = "select * from messages WHERE id=$id";$res = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());$num_msgs = mysql_num_rows($res);echo num_msgs; ?>[/code][*][code]<?php$q = "select count(*) as num_msgs from messages WHERE id=$id";$res = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());$rw = mysql_fetch_assoc($res);echo $rw['num_msgs']; ?>[/code][/ol]Ken Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 3, 2006 Author Share Posted March 3, 2006 Thank you ken How can i put this in a function and echo the funtion were i want it cheers.my example[code]<?phpfunction counting();{$q = "select count(*) as num_msgs from messages WHERE id=$id";$res = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());$rw = mysql_fetch_assoc($res);echo $rw['num_msgs']; }counting();?>[/code] Quote Link to comment Share on other sites More sharing options...
wickning1 Posted March 3, 2006 Share Posted March 3, 2006 "function counting();"just lose the semicolon:function counting() Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 3, 2006 Author Share Posted March 3, 2006 Can you kindly take a look, I got the function in the correct order of code but know i get a database error, But without the function the code is correct, What can you see wrong please thank you.[code]<?php $sql_host = 'xxxx'; //add host information here (localhost, mysql.host.com, etc.)$sql_un = 'xxxx'; //add your user name here$sql_pass = 'xxxx'; //add your password here$sql_db = 'freedating'; //add your database name heremysql_connect("$sql_host", "$sql_un", "$sql_pass") or die ("Could not connect to database");mysql_select_db($sql_db) or die ("Could not select database");function counting(){$q = "select count(*) as num_msgs from membercomments WHERE id=$id";$res = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());$rw = mysql_fetch_assoc($res);echo $rw['num_msgs'];} counting()?>[/code]This is the error i get but only in the function method overwise no error.[code]Problem with query: select count(*) as num_msgs from membercomments WHERE id=You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 3, 2006 Author Share Posted March 3, 2006 I got the function to work but is it the correct way thank you.[code]<?php function counting($count){$sql_host = 'xxxx'; //add host information here (localhost, mysql.host.com, etc.)$sql_un = 'xxxx'; //add your user name here$sql_pass = 'xxxx'; //add your password here$sql_db = 'freedating'; //add your database name heremysql_connect("$sql_host", "$sql_un", "$sql_pass") or die ("Could not connect to database");mysql_select_db($sql_db) or die ("Could not select database");$q = "select count(*) as num_msgs from membercomments WHERE id=$id";$res = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());$rw = mysql_fetch_assoc($res);echo $rw['num_msgs'];} echo $count?>[/code] Quote Link to comment 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.