Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    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

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    $sql = mysql_query("SELECT * FROM 'beers' WHERE id='$id'") or die(mysql_error());
    It's your responsibility to die(), if necessary!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    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 .

  • #5
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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 :)

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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 :)

  • #7
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •