+ Reply to Thread
Results 1 to 7 of 7

Thread: PHP / MySQL error

  1. #1
    lambada's Avatar
    lambada is offline x10 Elder lambada is an unknown quantity at this point
    Join Date
    Mar 2006
    Location
    Caister, Gt Yarmouth, Norfolk, ENGLAND
    Posts
    1,222

    PHP / MySQL error

    Little error in my PHP code I can't for the life of me figure out.
    Anyone out there know how to solve it??

    Heres the error:
    Catchable fatal error: Object of class mysqli could not be converted to string in /home/lambada/public_html/secretconfessions/dev/includes/entry/view.php on line 5
    Here's the code

    PHP Code:
    $db mysqli_connect("localhost""USER""PASS""DB") or die("Database Unavailable");
    $query "SELECT id, text, datetime, ipaddress, user FROM entries WHERE moderated = 1 ORDER BY id DESC";
    $selectentries mysqli_query("$db""$query"); 
    Any ideas?

    Thanks

    lambada (BTW: I removed the db details. I'm not THAT idiotic :P

    EDIT: Apparently I am - I forgot to say which was line 5. *looks sheepish* Line 5 is the
    PHP Code:
    $selectentries mysqli_query("$db""$query"); 
    line
    Last edited by lambada; 04-18-2007 at 02:36 PM.
    Lambada - the former Account Manager (before I resigned)




  2. #2
    Brandon's Avatar
    Brandon is offline Former Senior Account Rep Brandon is on a distinguished road
    Join Date
    Jun 2006
    Location
    Tewksbury, MA
    Posts
    9,589

    Re: PHP / MySQL error

    Which line is line 5?
    Thanks,
    Brandon Long

  3. #3
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: PHP / MySQL error

    I don't have time to test this out for myself.. But.. I believe I know what's going on.

    PHP Code:
    $selectentries mysqli_query("$db""$query"); 
    $db contains a 'mysqli-object.' In your mysqli_query(), you're putting that variable's value between double-quotes, which would make it look like this when PHP attempts to use it:

    PHP Code:
     $selectentries mysqli_query("[MySQLi Object]""$query"); 
    It's attempting to convert the mysqli object to a string, which.. Cannot be done.

    Try to take out the quotes around $db and see if you still get the error.

  4. #4
    Micro is offline Retired staff (11-12-2008) Micro is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    West Midlands
    Posts
    1,301

    Re: PHP / MySQL error

    Yeh, just remove the quotes from the $db and $query, they are variables so they can just be there on their own.
    Micro

  5. #5
    lambada's Avatar
    lambada is offline x10 Elder lambada is an unknown quantity at this point
    Join Date
    Mar 2006
    Location
    Caister, Gt Yarmouth, Norfolk, ENGLAND
    Posts
    1,222

    Re: PHP / MySQL error

    Ok, I did what you guys asked and it didn't print out anything, then I modified my code to use a while loop instead of foreach at Brandons suggestion, and it still didn't work. The final piece of the puzzle was that I was using mysqli_fetch_row which returns numerically indexed arrays, when I was referring in my code to string indexed arrays so I changed it to mysqli_fetch_assoc and it works like a charm.

    Thanks for all your help guys.
    Lambada - the former Account Manager (before I resigned)




  6. #6
    Zenax's Avatar
    Zenax is offline Lord Of The Keys Zenax is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    The Brilliant United Kingdom
    Posts
    1,339

    Re: PHP / MySQL error

    well i always thought a query would be done like this, but then again im a noob to PHP so im not entirely sure!

    PHP Code:

    include("conn.inc.php");

    $connection mysql_connect($hostname,$username,$password)
        or die(
    "Sorry Cannot Connect");
        
    $dbselect mysql_select_db("$dbname",$connection)
        or die(
    "Sorry Cannot Connect to the db");

    $style mysql_query("SELECT stylesheet FROM style");        // Queries the database for the stylesheet table 
    while ($stylerow mysql_fetch_array($style,MYSQL_ASSOC))     // Chooses the row

    print $stylerow{'stylesheet'};                                // Prints out the stylesheet to the page 
    Please correct me if I am wrong, as I am still learning PHP so the more I learn the better I get at helping people with their problems!
    Last edited by Zenax; 04-21-2007 at 08:10 AM.
    Regards,
    Zenax

  7. #7
    t2t2t's Avatar
    t2t2t is offline x10 Elder t2t2t is an unknown quantity at this point
    Join Date
    Sep 2006
    Location
    Europe, Estonia
    Posts
    690

    Re: PHP / MySQL error

    Quote Originally Posted by Zenax View Post
    well i always thought a query would be done like this, but then again im a noob to PHP so im not entirely sure!

    PHP Code:

    $username 
    "root";
    $password "1234";
    $hostname "localhost";

    $db_conn mysql_connect($username$password$hostname)
           or die(
    "Cannot connect!");

    $query "SELECT id, text, datetime, ipaddress, user FROM entries WHERE moderated = 1 ORDER BY id DESC";

    $sql mysql_select_db($query$db_conn)
          or die(
    "Cannot Connect!"); 
    Please correct me if I am wrong, as I am still learning PHP so the more I learn the better I get at helping people with their problems!
    You gotta work on it...

    PHP Code:
    <?
    mysql_connect
    ('HOST','USERNAME','PASSWORD') or die("Cannot connect!");
    mysql_select_db('table') or die("Cannot Connect!");
    $query "SELECT id, text, datetime, ipaddress, user FROM entries WHERE moderated = 1 ORDER BY id DESC";
    $answer mysql_query($query);
    This post has been marked spam 52 times.


+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  3. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  4. PHP MySQL Error
    By Salvatos in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 08-07-2006, 08:27 AM
  5. vBulletin mySQL error!
    By dphiance in forum Free Hosting
    Replies: 3
    Last Post: 06-11-2005, 02:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers