This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
Can someone explain how I would output the result of the sql below? currently getting 'Object of class mysqli_result could not be converted to string'.
$sql = ("SELECT AVG(ab_satisfactionScore) AS AverageSatisfactionScore
FROM tbl_appointmentsbooked;");
$result = mysqli_query($connection, $sql);
echo ($result);
Error because you are echoing an object, so try like this,
while($res = mysqli_fetch_array( $result )) {
echo $res['AverageSatisfactionScore'];
}
Use any of mysqli_fetch_*() functions (or in OOP style: $result->fetch_*()) to retrieve the results from the mysqli_results object ($results).
See mysqli_result documentation on the various methods and their uses.
because you try to use echo to print object, and it used to print string only, you should use:
print_f($result);
instead of
echo ($result);
Related
This question already has answers here:
Single Value Mysqli [duplicate]
(8 answers)
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 2 years ago.
I am having trouble extracting information from a table I have created in MYSQL. I am trying to extract the last entry id number of an individual and want to store it into a variable called $person_id.
So far, I have the following:
$res = mysql_query("SELECT max(person_id) FROM Persons;");
$person_id= mysql_query($res,$conn);
echo $person_id;
Nothing shows up when I try to print the variable name person_id.
The connection to the database works fine since I am able to insert data from a form I created. Any advice?
mysql_query was deprecated in PHP 5.5.
It will return a resource on success, or FALSE on error.
So, instead of echo, you could first try a var_dump($person_id).
Source : https://www.php.net/manual/en/function.mysql-query.php
First, avoid using mysql this extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. Now back to your question, you are printing out the wrong value, your query using mysqli should look like this
$res = "SELECT max(person_id) FROM Persons;";
$person = mysqli_query($conn, $res);
echo $person;
this will return nothing because the result is an array(mysqli object) it cannot be converted to string just like that but, using print_r we can print out the value of person as follows
print_r($persons);
you should get something like this
mysqli_result Object ( [current_field] => 0 [field_count] => count [lengths] => [num_rows] => rows [type] => 0 )
the count and rows represents, depending on the results the number of fields and rows used in the query.
To print out the $person variable correctly, as in your query above it should be
$res = "SELECT max(person_id) FROM Persons;";
$result = mysqli_query($conn, $res);
//fetch the database values as an asscoiative array
while($row = mysqli_fetch_assoc($result))
{
$person_id = $row['person_id'];
}
echo $person_id;
this should print the value you wanted
You must have got some error because you are missing something here.
Thats how it should work for you.
$res = "SELECT max(person_id) FROM Persons";
$result = mysql_query($res,$conn);
list($person_id) = mysql_fetch_row($result);
echo $person_id;
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 3 years ago.
I'm trying to echo the result of a mysqli_query however I keep getting the error 'Catchable fatal error: Object of class mysqli_result could not be converted to string' on line 'echo $result;'.
Is there any way I can convert it into a string so that it can be echoed? (P.S sorry if this is easy, I'm new to coding.)
My database is successfully connected and the SQL statements definitely work.
$sql= "SELECT ImageURL FROM `unnormalisedtable` WHERE Yeargroup = 9 ORDER BY RAND() LIMIT 1" ;
$result = mysqli_query($db, $sql);
echo $result;
The expected output is that the result of my SQLi query will be printed on screen, however the error is generated instead. Thanks for any help in advance.
Since you limit the selection to one entry use
$row = mysqli_fetch_array($result);
echo $row['ImageURL'];
If you select more than one entry loop over the result.
while($row = mysqli_fetch_array($result)) {
echo $row['ImageURL'];
}
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
Can someone explain how I would output the result of the sql below? currently getting 'Object of class mysqli_result could not be converted to string'.
$sql = ("SELECT AVG(ab_satisfactionScore) AS AverageSatisfactionScore
FROM tbl_appointmentsbooked;");
$result = mysqli_query($connection, $sql);
echo ($result);
Error because you are echoing an object, so try like this,
while($res = mysqli_fetch_array( $result )) {
echo $res['AverageSatisfactionScore'];
}
Use any of mysqli_fetch_*() functions (or in OOP style: $result->fetch_*()) to retrieve the results from the mysqli_results object ($results).
See mysqli_result documentation on the various methods and their uses.
because you try to use echo to print object, and it used to print string only, you should use:
print_f($result);
instead of
echo ($result);
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
I am getting the error:
Object of class mysqli_result could not be converted to string.
Code:
<?php
$con=mysqli_connect("78.46.51.231","root","","multicraft_daemon");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = ("select sum(`memory`) from `server`;");
$result = mysqli_query($con, $sql);
echo $result; //$result is mysqli_result and can't be forced to string.
?>
What is the correct way to do this?
You cannot directly output the result of a query. Use:
$sql = ("select sum(`memory`) AS memTotal from `server`");
// Show used memory
$result = mysqli_query($con, $sql);
echo $result->fetch_object()->memTotal;
The $result variable holds an object (of type mysqli_result), from which you can fetch the scalars you need to output.
$result is a result object. From thje manual for mysqli_query():
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or
EXPLAIN queries mysqli_query() will return a mysqli_result object. For
other successful queries mysqli_query() will return TRUE.
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
I know there have been asked lots of similair questions like this one, but I just can't translate it to my problem so thats why I ask again. The code used to work fine but since the mysql is going to be deprecated I wanted to translate to mysqli.
I receive the following error when trying to read something from database: Catchable fatal error: Object of class mysqli_result could not be converted to string. It refers to line 12, which is
echo $result;
FULL CODE:
$previd ="10";
$query="SELECT * FROM contacts WHERE id='$previd'";
$result = $mysqli->query($query);
echo $result;
$num=$result->num_rows;
$mysqli->close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
$content=mysql_result($result,$i,"content");
echo "<u>$id</u><b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>$content";
$i++;
}
How can I solve this?
$num=$result->num_rows;
echo "<b><center>Database Output</center></b><br><br>";
while ($row = $result->fetch_assoc()) {
echo "<u>".$row['id']."</u><b>".$row['first']."</b>"; //etc...
}
is much easier.
That is because mysqli_result is a result set. I don't know what you would expect to gain from echoing it, just as you wouldn't get anything meaningful from echoing the result set of a mysql_query query which uses a SELECT.
You need to use the various class methods to access data in the result set.
http://php.net/manual/en/class.mysqli-result.php