View Full Version : Help for a php newbie...

01-09-2010, 07:55 AM
I'm working on a beer review site. I need a page that can draw data from my db and input it into an html, but different info for different beer pages. It's similar to facebook's system for profiles. This doesn't include the html it's just , but I am getting the error as follows:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource ....on line 15

Oh and my DB connect info DOES work, just edited to not reveal info

$con = mysql_connect("********","*******","******"); //connects to DB
if (!$con)
die('COULD NOT CONNECT! ' . mysql_error());

$id = $_GET['id'];

$sql = mysql_query("SELECT * FROM 'beers' WHERE id='$id'");

while($row = mysql_fetch_array($sql)){

$beername = $row['beername'];
$date = $row['date'];
$style = $row['style'];
$brewer = $row['brewer'];


I know this is total newb stuff, but what am I doing wrong? I've looked at other forums with this error but cannot find a problem that fits it. Basically, I am just trying to declare these variables so as to use them later in an html file. Any help would be greatly appreciated. Thanks folks!

01-09-2010, 08:19 AM
It means you have an error in your sql query try removing the single quotes from arround your table name, also mare sure the value you are getting from you get variable is what you're expecting

01-09-2010, 08:44 AM
$sql = mysql_query("SELECT * FROM 'beers' WHERE id='$id'") or die(mysql_error());

It's your responsibility to die(), if necessary!

Deacon Frost
01-09-2010, 01:41 PM
Try something like this:

// Get the ID
$id = $_GET['id'];

// Set connection details.
$db_hostname = "localhost"; // Host (Typically 'localhost')
$db_username = ""; // Username
$db_pass = ""; // Password
$db_name = ""; // Database Name

// This initializes the connection to the database.
$con = mysql_connect($db_hostname, $db_username, $db_pass) or die('Could not connect to mysql server.');

// Connect to the specific database.
mysql_select_db($db_name, $con) or die('Could not select database.');

// If there is an ID from above.
if($id) {

// Select variable, with LIMIT 1, just in case.
$sql = "SELECT * FROM `beers` WHERE `id`='$id' LIMIT 1";

// Query variable, with error handling.
$result = mysql_query($sql) or die('The error was: ' . mysql_error() . '<br />The query was: ' . $sql);

// Begin the while loop, also executing the query variable.
while($row = mysql_fetch_assoc($result)) {

// Set Variables...
$beername = $row['beername'];
$date = $row['date'];
$style = $row['style'];
$brewer = $row['brewer'];

A little more complex, but it's a bit easier to edit, and has some error handling thrown in there :).

01-09-2010, 03:49 PM
You are missing the mysql_select_db line

01-09-2010, 03:50 PM
Also 'beers' should not have the quotes around it in your SQL. Either change it to backticks `beers` or remove them completely beers

01-09-2010, 06:30 PM
WOW! You all are awesome!

It turned out to be a combonation of all the answers you folks provided. Thanks so much! I have always looked over the boards but never posted myself. But wow you all were so helpful, nice, and FAST! And thanks for not all making me feel like a dumbass for not knowing this. You guys do this stuff just out of kindness and you made my life so much less frusturating so I really appreciate it and keep up the great work!

Thanks again!