Jump to content

Archived

This topic is now archived and is closed to further replies.

redarrow

count table entrys

Recommended Posts

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]

Share this post


Link to post
Share on other sites

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

?>
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thank you ken

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

my example
[code]
<?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();
?>
[/code]

Share this post


Link to post
Share on other sites
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 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()
?>
[/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]

Share this post


Link to post
Share on other sites
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 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
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.