MySQL ERROR

Discussion in 'PHP' started by matiohi, Dec 6, 2008.

  1. #1
    I've been trying to make a (very) simple shopping system where somebody can see the current orders, here is the code:
    
    <?php
      //create short variable name
      $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
    ?>
    <html>
    <head>
      <title>Bob's Auto Parts - Customer Orders</title>
    </head>
    <body>
    <h1>Bob's Auto Parts</h1>
    <h2>Customer Orders</h2>
    <?php
    $mysql = mysqli_connect( 'localhost', 'bob', 'bob' );
    
    if(!$mysql)
    
    {
    
      echo 'Cannot connect to database.';
    
      exit;
    
    }
    
    // select the appropriate database
    
    $selected = mysqli_select_db( $mysql, 'bob' );
    
    if(!$selected)
    
    {
    
      echo 'Cannot select database.';
    
      exit;
    
    }
    $result = $mysql->query('select * from orders;');
    $eresult = settype($result, string);
    echo $eresult;
    
    ?>
    </body>
    </html>
    
    PHP:
    when I view the page I get this error message:
    Catchable fatal error: Object of class mysqli_result could not be converted to string in /var/www/bobs/vieworders.php on line 27

    something to do with it not being a string?
    Any help would be great.
     
    matiohi, Dec 6, 2008 IP
  2. CommuniTriper

    CommuniTriper Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It means you can't convert the result you get (the result object) into a simple string.

    Instead you have to recourse through the result set

    try something like this:

    while ($row = mysql_fetch_array($result)) {
    echo "{$row["orderid"]}<br>";
    }

    Where orderid is just an assumption of mine for your field's name, just replace it with whatever field name/s you use...
     
    CommuniTriper, Dec 6, 2008 IP
  3. matiohi

    matiohi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but now I get this message:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/bobs/vieworders.php on line 27
     
    matiohi, Dec 6, 2008 IP
  4. lordofthelake

    lordofthelake Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This should be correct (you can't mix mysql and mysqli functions):
    
    while($row = $result->fetch_object()) {
      echo $row->orderid, '<br />';
    }
    
    Code (php):
     
    lordofthelake, Dec 7, 2008 IP
  5. CommuniTriper

    CommuniTriper Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    right, my bad, lordofthelake what would you say is the main difference between the two methods? which one is better to use?
     
    CommuniTriper, Dec 7, 2008 IP
  6. lordofthelake

    lordofthelake Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, there isn't the "best" method... mysql_* functions and mysqli are just two different extensions for doing the same thing.

    mysql:
    • Procedural style
    • A bit faster (but the difference is negligible)
    • "Traditional" way to interact with MySql
    • Will be dropped in PHP6
    mysqli (MySql Improved):
    • Both OO and procedural style supported
    • Present since PHP4, it's the preferred method to use in PHP5
    • Gives easier access to many new MySql functions (for example, transactions and prepared statements)
    • Will benefit of the new mysqlnd driver, soon.
     
    lordofthelake, Dec 7, 2008 IP
  7. matiohi

    matiohi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks it worked!
    :)
     
    matiohi, Dec 7, 2008 IP
  8. lordofthelake

    lordofthelake Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Glad to hear this :)
     
    lordofthelake, Dec 7, 2008 IP