Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2010, 06:55 AM   PM User | #1
bosstones
New to the CF scene

 
Join Date: Jan 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
bosstones is an unknown quantity at this point
Help for a php newbie...

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

PHP Code:
<?php
$con 
mysql_connect("********","*******","******"); //connects to DB
if (!$con

die(
'COULD NOT CONNECT! ' mysql_error()); 

    (
$_GET['id']); 
    
    
$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!
bosstones is offline   Reply With Quote
Old 01-09-2010, 07:19 AM   PM User | #2
ninnypants
Regular Coder

 
ninnypants's Avatar
 
Join Date: Apr 2008
Location: Utah
Posts: 504
Thanks: 10
Thanked 47 Times in 47 Posts
ninnypants is an unknown quantity at this point
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
ninnypants is offline   Reply With Quote
Old 01-09-2010, 07:44 AM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Code:
$sql = mysql_query("SELECT * FROM 'beers' WHERE id='$id'") or die(mysql_error());
Quote:
It's your responsibility to die(), if necessary!
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 01-09-2010, 12:41 PM   PM User | #4
Deacon Frost
Regular Coder

 
Deacon Frost's Avatar
 
Join Date: Feb 2008
Location: Between the Lines
Posts: 279
Thanks: 31
Thanked 4 Times in 4 Posts
Deacon Frost is on a distinguished road
Try something like this:

PHP Code:
<?php
// 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 .
Deacon Frost is offline   Reply With Quote
Old 01-09-2010, 02:49 PM   PM User | #5
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
You are missing the mysql_select_db line
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-09-2010, 02:50 PM   PM User | #6
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
Also 'beers' should not have the quotes around it in your SQL. Either change it to backticks `beers` or remove them completely beers
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 01-09-2010, 05:30 PM   PM User | #7
bosstones
New to the CF scene

 
Join Date: Jan 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
bosstones is an unknown quantity at this point
Thank you so much!

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!
bosstones is offline   Reply With Quote
Reply

Bookmarks

Tags
error, mysql, newbie, php, query

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:37 PM.


Advertisement
Log in to turn off these ads.