Page 3 of 3
Posted: Wed Oct 11, 2006 3:20 pm
by Javrixx
Ok, so what do I do now? Is this just a lost cause and I should give up on it? And sorry, but I don't understand most php commands yet, I usually have to look up each and every one, and then use a script and mess with it so I learn by trial and error, so I don't know what the mysql_fetch_array.
Posted: Wed Oct 11, 2006 3:35 pm
by RobertGonzalez
Here is a very basic data fetch procedural snippet:
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>';
}
?>
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().
Posted: Wed Oct 11, 2006 5:46 pm
by Javrixx
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:
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'];
}
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.
Posted: Wed Oct 11, 2006 5:49 pm
by Javrixx
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.

Posted: Wed Oct 11, 2006 7:12 pm
by RobertGonzalez
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.