PDA

View Full Version : There are X amount of entries



adamFTW
08-12-2007, 04:04 PM
I have the following code, how could I make it so above the entries, it would say "There are a total of XX entries".

Is it possible?
Heres my code:

<?php
// The database connetcion
include "config.php";

$sql = "SELECT * FROM petition";
$check = mysql_query("SELECT name, grade, comment FROM petition") or die("Sorry, the data couldn't be displayed. Please check back later.");
// The above variables will be used to check our connection to MySQL.

while ($display = mysql_fetch_array($check)) {

$name = $display['name'];
$grade = $display['grade'];
$comment = $display['comment'];

// Above are variables defined for our Name, Gradeand Comment.

echo "
<strong>Name:</strong> $name
<br>
<strong>Grade / Other:</strong> $grade
<br>
<strong>Comment:</strong><br>$comment
<br>
__________________________________________________ __________<br /><br />
";

}
?>

adamFTW
08-12-2007, 04:57 PM
Anybody?

ZAG
08-12-2007, 05:28 PM
I didnt really know what you meant but maybe this will work




$query = mysql_query("SELECT * FROM petition");
$entries = mysql_num_rows($query);
echo("There are $entries entries.");

RYANNNNN
08-12-2007, 05:40 PM
What entries? Total petitions? Total comments?

Either way..
$query = mysql_query("select count(*) from petition");
$result = mysql_result($query, 0);
echo $result;

Don't use the suggestion above, not being mean to him but whilst it'll work, it isn't kind to the mysql load.

Beau
08-12-2007, 11:39 PM
I wouldn't use either. You're already querying the database once, there isn't a need to do so a second time.


<?php
// The database connetcion
include "config.php";

$check = mysql_query("SELECT name, grade, comment FROM petition") or die("Sorry, the data couldn't be displayed. Please check back later.");

$num = mysql_num_rows($check);

// The above variables will be used to check our connection to MySQL.

while ($display = mysql_fetch_array($check)) {

$name = $display['name'];
$grade = $display['grade'];
$comment = $display['comment'];

// Above are variables defined for our Name, Gradeand Comment.

echo "
<strong>Name:</strong> $name
<br>
<strong>Grade / Other:</strong> $grade
<br>
<strong>Comment:</strong><br>$comment
<br>
__________________________________________________ __________<br /><br />
";

}
?>

That way, $num holds the amount of rows returned.

Want to hide these adverts? Register an account for free!