PHP / MySQL error

lambada

New Member
Messages
2,444
Reaction score
0
Points
0
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:
$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:
$selectentries = mysqli_query("$db", "$query");
line
 
Last edited:

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Which line is line 5?
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
I don't have time to test this out for myself.. But.. I believe I know what's going on.

PHP:
$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:
 $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.
 

Micro

Retired staff <i> (11-12-2008)</I>
Messages
1,301
Reaction score
0
Points
36
Yeh, just remove the quotes from the $db and $query, they are variables so they can just be there on their own.
 

lambada

New Member
Messages
2,444
Reaction score
0
Points
0
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.
 

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
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:
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:

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
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:
$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:
<?
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);
 
Top