COUNT not displaying<

admin

Administrator
Staff member
I'm trying to get it to count the number of rows affected in a databse. And it's not working

<?php
// Database Connection
require_once("includes/dbconnect.php");
//include functions
require_once("includes/functions.php");

if(empty($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}


$display = $_GET['display'];
if(issetAndis_int($display, 5, 50)){
$max_results = $display;
}else{
$max_results = 10;
}

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

$month = date("m");


// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM games WHERE Month(DATE) = $month"),0) or die(mysql_error());
/*
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
if($page > $total_pages){
header("Location: newest.php?display=$display&page=1");
exit;
}
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
/*
$sql = "SELECT DATE_FORMAT(DATE, '%M %D %Y') AS NewDate, G.TITLE, G.description, G.URL, G.CategoryID, G.ID, GC.CLICKS, GC.monthclicks
FROM games G INNER JOIN game_clicks GC ON
GC.GAME_ID = G.ID
ORDER BY G.DATE DESC
LIMIT $from, $max_results
";*/
echo $month.'<br>';
echo $from;
?>
</body>
</html>


as you can see i've commented out some stuff to figure out what's wrong, and it's not echoing either $month or $from. I don't know why, any idea?for one your query should have a ' around the $month variable as you always need those with strings.

other than that it works for me. dispalys 07 for the month. comment out the query and you will see it.Yah but when I uncomment the query line nothing comes up..I don't see how that would effect it, all you are doing is echoing this variable

$month = date("m");

the query has nothing to do with itOhh, sorry, what I'm trying to echo is $total_results and that's not what's coming up.echo $total_results[0];

or echo $total_results;

can't remember as I don't use mysql_resultNot coming up with anything.
code

<?php
// Database Connection
require_once("includes/dbconnect.php");
//include functions
require_once("includes/functions.php");

if(empty($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}


$display = $_GET['display'];
if(issetAndis_int($display, 5, 50)){
$max_results = $display;
}else{
$max_results = 10;
}

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

$month = date("m");


// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM games WHERE Month(DATE) = '$month'"),0) or die(mysql_error());
/*
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
if($page > $total_pages){
header("Location: newest.php?display=$display&page=1");
exit;
}
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
/*
$sql = "SELECT DATE_FORMAT(DATE, '%M %D %Y') AS NewDate, G.TITLE, G.description, G.URL, G.CategoryID, G.ID, GC.CLICKS, GC.monthclicks
FROM games G INNER JOIN game_clicks GC ON
GC.GAME_ID = G.ID
ORDER BY G.DATE DESC
LIMIT $from, $max_results
";*/
echo $month.'<br>';
echo $total_results[0];
?>
</body>
</html>


and the only code it's displaying when
i view source is <html><body></body></html>why are you using mysql_result? you want more than 1 results so mysql_result will not work.

if all you wnat is the total then use mysql_num_rows

$total_results = mysql_num_rows(mysql_query("SELECT id FROM games WHERE Month(DATE) = '$month'"),0) or die(mysql_error());

change id to be what ever you want, more than likely it sould be your auto-incremented columnargh, still not displaying anything.

<?php
// Database Connection
require_once("includes/dbconnect.php");
//include functions
require_once("includes/functions.php");

if(empty($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}


$display = $_GET['display'];
if(issetAndis_int($display, 5, 50)){
$max_results = $display;
}else{
$max_results = 10;
}

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

$month = date("m");


// Figure out the total number of results in DB:
$total_results = mysql_num_rows(mysql_query("SELECT ID FROM games WHERE Month(DATE) = '$month'")) or die(mysql_error());
/*
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
if($page > $total_pages){
header("Location: newest.php?display=$display&page=1");
exit;
}
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
/*
$sql = "SELECT DATE_FORMAT(DATE, '%M %D %Y') AS NewDate, G.TITLE, G.description, G.URL, G.CategoryID, G.ID, GC.CLICKS, GC.monthclicks
FROM games G INNER JOIN game_clicks GC ON
GC.GAME_ID = G.ID
ORDER BY G.DATE DESC
LIMIT $from, $max_results
";*/
echo $month.'<br>';
echo $total_results;
?>
</body>
</html>very strange. did you comment out the query and you got nothing?

ok, do this

"SELECT ID FROM games WHERE Month(DATE) = '07'"

do that query in phpmyadmin and see if it returns something. it looks funny to meIt's working now.

But yah, i commented out the query and got nothing.

I'm not sure what the problem was after I used mysql_num_rows like you suggested, but when I put in 07 instead of $month it worked. And now $month works also.

Thanksvery strange. it seems that he query was messing things up. pretty weird
 
Back
Top