PHP Error
Results 1 to 4 of 4

Thread: PHP Error

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2

    PHP Error

    I get this error when running this code Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource. The code is
    Code:
    <?php
    
    
    function fetch_products(){
    $sql = "SELECT 'product_id','product_name' FROM 'products'";
    
    $result = mysql_query($sql);
    $products = array();
    
    while(($row = mysql_fetch_assoc($result)) !== false){
    $products[$row['product_id']] = $row['product_name'];
    }
    
    return $products;
    
    
    }
    
    
    
    
    ?>
    I read around and someone said this is because the code isnt linking properly to the DB. However if I do
    Code:
    <?php
    
    
    function fetch_products(){
    
    $result = mysql_query("SELECT * FROM products");
    $products = array();
    
    while(($row = mysql_fetch_assoc($result)) !== false){
    $products[$row['product_id']] = $row['product_name'];
    }
    
    return $products;
    
    
    }
    
    
    ?>

  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Then the code works fine however as I go on coding using this tut http://www.youtube.com/watch?v=DZRxxAGCSlI. The actual voting part doesnt work if needed i can post voting part

  3. #3
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,670
    Welcome to PHPBuilder.

    Please consider learning and using the mysqli_ (MySQL Improved) API/function set instead of the deprecated mysql_ functions, which have been outdated for quite some time.

    I'd recommend calling mysqli_error() [mysql_error() using the old API as you are, above] to see if you can gain any insight:

    PHP Code:
    $sql "SELECT 'product_id','product_name' FROM 'products'";

    $result mysql_query($sql);

    if (!
    $result) {
       echo 
    "No result ... mysql said: ".mysql_error();
    } else {
       
    $products = array();
       while((
    $row mysql_fetch_assoc($result)) !== false){ 
          
    $products[$row['product_id']] = $row['product_name'];
       }

    HTH,
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  4. #4
    Code Monkey
    Join Date
    Oct 2002
    Location
    Delaware
    Posts
    308
    You're using regular quotes as opposed to backticks.

    Try simply:

    SELECT product_id, product_name FROM products
    Lackey @ Large

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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