Code: Select all
<?php function ItemCount()
{
//database connection
$sql = "SELECT COUNT(*) FROM pendants";
return mysql_query($sql);
}
echo ItemCount();
?>Moderator: General Moderators
Code: Select all
<?php function ItemCount()
{
//database connection
$sql = "SELECT COUNT(*) FROM pendants";
return mysql_query($sql);
}
echo ItemCount();
?>Code: Select all
<?php function ItemCount()
{
//database connection
$sql = "SELECT COUNT(*) FROM pendants";
$r=mysql_query($sql);
return $r[0];
}
echo ItemCount();
?>Code: Select all
$sql = "SELECT COUNT(*) FROM pendants";
$count=mysql_result($sql, 0);
echo "They have: $count";Code: Select all
<?php function ItemCount()
{
//database connection
$sql = "SELECT COUNT(*) FROM pendants";
$r = mysql_query($sql);
return mysql_result($r, 0);
}
echo ItemCount();
?>Not in this case.crystal ship wrote:I think mysql_num_rows($result) will do good for your case.
Code: Select all
<?php
$sql = "SELECT COUNT(*) AS `row_count` FROM pendants";
if (! $result = mysql_query($sql)) {
die('Could not run the query: ' . mysql_error());
}
// Get the resulting query return
$res = mysql_fetch_array($result);
// Grab the row from the result set you want
$count = $res['row_count'];
?>Code: Select all
<?php
$sql = "SELECT * FROM pendants";
if (! $result = mysql_query($sql)) {
die('Could not run the query: ' . mysql_error());
}
// Count the result set count
$count = mysql_num_rows($result);
?>Or instead of using an alias, just take the first array element since you'll likely only be getting the COUNT().Everah wrote:You could do two things here. The first would be (and in my opinion, the best choice) to read the count into an alias in the query
Code: Select all
$result = mysql_query("SELECT COUNT(*) FROM`tableName`");
$data = mysql_fetch_row($result);
echo $data[0];