Jump to content

Mysqli_result As String


23.12.2012

Recommended Posts

Today, I came across this error

Catchable fatal error: Object of class mysqli_result could not be converted to string in /Library/WebServer/Documents/sangym2/functions/user_title.php on line 12

What I'm trying to do is perform a query inside a function, in order to get the name field using the provided id. As far as I've read, the issue is triggered by the mysqli_query() function, which does not return a string, so the result of the function cannot be echo-ed. So how can I convert it to string before I display it?

Link to comment
Share on other sites

The best way to do this is by making the mysqli as a class:

<?php//Database connection$db = array (	'host' => $db_host,	'user' => $db_user,	'pass' => $db_pass,	'dbname' => $db_name);$mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['dbname']);if(mysqli_connect_errno()){	trigger_error('Fout bij verbinding: '.$mysqli->error);}//Sample query (SELECT)  $sql = "SELECT * FROM testtable WHERE tester='test'";  if(!$test= $mysqli->query($sql))  {	trigger_error('Fout in query: '.$mysqli->error);  }  else  {	while($testinfo= $test->fetch_assoc())	{	  echo $testinfo['test_name'].'<br />';	}}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...