View Full Version : I need help with simple PHP/SQL
Derek Zoolander
04-27-2007, 09:02 PM
This should be easy. I am just trying to output all the scores in a table named 'scores'. There are 2 attributes, email (primary key) and score. I want to output every score. There is only 1 record in the scores table which is '1', but it should still print it out.
Here is my php code:
<?php
$link = mysql_connect("xxxxx", "xxxxx", "xxxxx") or die("connect
error:".mysql_error());
Print "Successfully Connected.\n";
$db = "xxxxx";
mysql_select_db($db, $link) or die("select DB error:".mysql_error());
$result = mysql_query("SELECT * FROM scores");
print "Here is the list of high scores: $result";
mysql_close($link);
?>
*X's represent my host, username, password
This is what I get as output:
Successfully Connected. Here is the list of high scores: Resource id #3
TIA
Fumigator
04-27-2007, 09:35 PM
What mysql_query() does is creates a "resource" (don't ask me to define that), but doesn't return any data. After the resource is created, then you can run fetches against it to retrieve the actual data.
Here are the common fetch functions you can use:
mysql_fetch_array() (http://us2.php.net/manual/en/function.mysql-fetch-array.php) -- returns one row from the result set in array form (actually returns two arrays, one with numeric index and one with associative index)
mysql_fetch_assoc() (http://us2.php.net/manual/en/function.mysql-fetch-assoc.php) -- returns one row from the result set in an associative array
mysql_result() (http://us2.php.net/manual/en/function.mysql-result.php) -- returns one column value from the result set
Read up on each in the PHP manual to determine which you should use.
Derek Zoolander
04-28-2007, 01:52 AM
This is what I get when I use mysql_result:
Warning: Wrong parameter count for mysql_result() in /home/media224/public_html/scores.php on line 10
$result = mysql_result("SELECT * FROM scores");
It'd be something along the lines of
$result = mysql_query("SELECT * FROM scores");
while($row = mysql_fetch_array($result))
{
// $row is now an array with the values from scores
// You can do print_r($row) to see what it contains
echo $row['email'] .' '. $row['score'];
}
PappaJohn
04-28-2007, 02:13 AM
I would do something along these lines:
<?php
$link = mysql_connect("xxxxx", "xxxxx", "xxxxx") or die("connect
error:".mysql_error());
Print "Successfully Connected.\n";
$db = "xxxxx";
mysql_select_db($db, $link) or die("select DB error:".mysql_error());
$result = mysql_query("SELECT * FROM scores");
while ( $row = mysql_fetch_assoc($result) )
{
print 'Here is the list of high scores: ' . $row['score'];
}
mysql_close($link);
?>
Edit: oops, mr_e beat me to it.
Derek Zoolander
04-28-2007, 09:49 PM
I got it working. Thanks a lot bros!:thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.