I have a small database, at the present time, built for basic tracking and it has 5 rows ...

trackid
datetime
uid
emailaddress
orgcodetrack

It's populate with 10 records test data from 3 users (uid)...

I need to return the total visits from each uid... Here's what I have so far and it returns nothing...

<?php

$uidTotal = mysql_connect("localhost", "user", "pass");
mysql_select_db("table", $uidTotal);

$result = mysql_query("SELECT * FROM tracking WHERE uid = $uid", $uidTotal);
$a = mysql_num_rows($result);

echo "$a \n";

?>

Can I run the SQL code inside the body of the page? Does/Can this code go inside the header? Is the code even written correctly? Do I split out the <?php echo $a ?> into the div I need the info from?

Any Help would be great!
Ted

I think your code has little problem, try following:

<?php

$uidTotal = mysql_connect("localhost", "user", "pass");
mysql_select_db("table", $uidTotal);

[B]$uid=3;[/B]

$result = mysql_query("SELECT * FROM tracking WHERE uid = [B]{$uid}[/B]", $uidTotal);
$a = mysql_num_rows($result);

echo "[B]{$a}[/B] \n";

?>

thanks for the reply urtrivedi...

I changed the SQL code a bit it wasn't pulling what I needed to pull... Since I already have the connection string in the header I took it out of the code...

<td>
<?php
												$trackuid = $row_tracking['uid'];
											
$sql = mysql_query("SELECT COUNT(uid) FROM tracking WHERE uid = {$trackuid}");

$a = mysql_num_rows($sql);
												echo "{$a}";

?>
</td>

I get the number 1 returned, which is strange, none of the values in the DB are 1...

Is there something screwy with my SQL code? I have a sinking suspicion that's the case...

Ted

Count is an aggregate function. It's not going to return anything from the database. It's only going to count the number of records matching your criteria and return the total.

that's what I want to return a total count of uid 1 = 400 times or uid 3 = 10 times...

but the COUNT is only returning the number 1...

Well, if the uid is a numeric datatype, you could use:

SELECT COUNT(uid) FROM tracking WHERE uid = $trackuid

If not numeric, you'll have to surround with quotes:

SELECT COUNT(uid) FROM tracking WHERE uid = "$trackuid"

I fixed it...

here's what worked to return total visits from one user...

<?php
$trackuid = $row_tracking['uid'];
												
$sql = "SELECT COUNT(uid) AS total FROM tracking WHERE uid = $trackuid"; 
$query = mysql_query($sql);

$recordset = mysql_fetch_assoc($query);
$count = $recordset["total"];
												
echo "$count";
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.