Help on count code
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Here is a very basic data fetch procedural snippet:
What this is doing is including the database connection routine from dbdetails.php, then reading a SQL instruction into a string ($sql), then passing that string to the mysql_query() function (which returns a result resource, not a result set), then finds the number of rows in the result using mysql_num_rows(), then loops the result by assinging the result to the array var $row by way of mysql_fetch_array(). During the loop, the database row values are sent to the screen using echo().
Code: Select all
<?php
require_once 'dbdetails.php'; // Handles the connection and such
$sql = "SELECT `firstname`, `lastname`, `emailaddress` FROM `site_users`";
if (!$result = mysql_query($sql))
{
die('Could not query the table: ' . mysql_error() . ' was found in this query ' . $sql);
}
$result_count = mysql_num_rows($result);
while ($row = mysql_fetch_array_result($result))
{
echo '<a href="mailto:' . $row['emailadress'] . '">Email ' . $row['firstname'] . ' ' . $row['lastname'] . '</a>';
}
?>Ok, I appreciate everyone's help, I really do. This board rocks, and I hope to repay the favor one day when I become and expert and know it like HTML. But unfortunately my problem isn't solved, so I'm looking at a different approach to get this done. It seems like it should work:
However, I get this when I try to run the page:
"Query failed: 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 Actual query: SELECT count(*) AS Count FROM results WHERE user_id="
and I have no idea why. I have 2 days left to figure this one last problem out, everything else is complete and working fine.
Code: Select all
$sql= 'SELECT * FROM '.$mysql['prefix'].'users WHERE username="'.$_SESSION['username'].'"';
if(!$result = mysql_query($sql))
{
die('The following MySQL query failed. User data could not be retrieved. '.$sql);
}
// assign the user info to variables
while (($row = mysql_fetch_array($result)) != false)
{
$firstname = $row['firstname'];
$userid = $row['id'];
}
$sql2 = 'SELECT count(*) AS Count FROM results WHERE user_id='.$id;
$result2 = mysql_query ($sql2) or die ("Query failed: " . mysql_error()
. " Actual query: " . $sql2);
while ($row = mysql_fetch_assoc ($result2))
{
$showresults = $row['Count'];
}"Query failed: 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 Actual query: SELECT count(*) AS Count FROM results WHERE user_id="
and I have no idea why. I have 2 days left to figure this one last problem out, everything else is complete and working fine.
Nevermind, that was totally my mistake, I had the $id instead of $userid. AND IT WORKS!!! WOOOHOOO!!! Almost 3 days trying to get this to work and it finally works. Thanks so much for eveyrone's help, I learned a lot which helped me do this. If anyone has any last advice or anything, please let me know. 
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Small piece of advice. Work a tad slower when you are about to get it right. I have done it a million times (using something liek $id when it should have been $userid) because I was so excited to see it that I forgot the right var.
Congratulations by the way. You are that much closer to becoming a PHP stud.
Congratulations by the way. You are that much closer to becoming a PHP stud.