Jump to content


Photo

count table entrys


  • Please log in to reply
6 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 March 2006 - 03:04 AM

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.

<?php 
$res = mysql_query("select * from messages WHERE id=$id") 
$row = mysql_fetch_array($res); 
echo count($row);  
echo $row[0]; 
echo count($row); 
?> 

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 March 2006 - 03:35 AM


I was using this code the member has only got 1 message but i receve 18 please help thank you.


<?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 here


mysql_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); 

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 March 2006 - 03:37 AM

There are at least two ways to do this...
[ol type=\'1\'][*]
<?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;  
?>
[*]
<?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'];  
?>
[/ol]
Ken


#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 March 2006 - 03:43 AM

Thank you ken

How can i put this in a function and echo the funtion were i want it cheers.

my example
<?php
function 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();
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 03 March 2006 - 06:08 AM

"function counting();"

just lose the semicolon:

function counting()

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 March 2006 - 10:38 AM

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.

<?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 here


mysql_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()
?>

This is the error i get but only in the function method overwise no error.

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

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 March 2006 - 10:58 AM

I got the function to work but is it the correct way thank you.
<?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 here


mysql_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
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users