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
Related
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 an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I an trying to query a mySQL database using a PHP function. Intermittantly, the function that I am using does not seem to return a result. As far as I can detect, it's not a null response, and based on the function, it doesn't seem to be returning an invalid result ( the return string Nuthin in this case ).
function GeneratePiece2 ($sqlstr){
$conn = new mysqli(gHOST, gUSER, gPASSWORD, gDATABASE);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query($sqlstr);
$returnable = "";
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo $row;
$returnable = $row;
}
} else {
return "nuthin";
}
$conn->close();
return $returnable;
}
I've been using the same SQL Query of
SELECT Name FROM Char_Name_full WHERE Male=1 AND DivisionID=12 ORDER BY RAND() LIMIT 1
Around 80% of the time I will receive a result of something like {"Name":"Christoph"} but 1 out of 10 returns nothing.
If someone is having problems with fetch_assoc() returning "Undefined index":
Check the indexes names (database Column name) - they are case sensitive.
Example:
For "SELECT Name FROM..." Your code must be $row["Name"]. Not NAME or name.
In your query, you are checking if the number of rows is greater than zero, but you are not doing anything to check if the value that is returned is Null. The behaviour that you are describing matches the scenario of at least 1 row in 'Name' that has a NULL value.
Note that I am not going to comment on your code structure or syntax here, but I have changed your response values to help identify / prove that a null value is the issue here.
Also, if your query is limited to a single row, then the while loop will only iterate once.
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// Echo out the values for Name in the result set
if(is_null($row["Name"], ))
echo '[NULL]';
else
echo $row["Name"];
// your application logic ;)
$returnable = $row;
}
} else {
return "no rows";
}
Please consider using a SQL IDE like Toad for MySQL along side your development, then you can visually inspect your data for these issues without writing code hacks :)
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:
remove duplicating fields in php-mysql result rows
(2 answers)
Closed 8 years ago.
Hey guys (and girls) I'm having a problem with arrays, this code below looks like duplicating each column inside a array! :/
<?php
//quantidade_de_registro
include("mysqlconfig.inc");
$query = "SELECT * FROM contas ";
$res = mysql_query($query);
while($row = mysql_fetch_array($res)){
$arr[] = $row;
}
echo json_encode($arr);
mysql_close($con);
?>
It will returns something like this:
[{"0":"5","ID":"5","1":"Zenny","Login":"Zenny","2":"Zeny","Nome":"Zeny","3":"daniel_queiroz789#hotmail.com","Email":"daniel_queiroz789#hotmail.com","4":"23021994","Senha":"23021994"}]
Each Column appears twice, But I need each column appears just once, a friend mine said that I need to re-parse the array and put it into the array, I don't know what it means or how I can do that :/
Please help :)
you can modify your script by adding a second parameter to the fetch
mysql_fetch_array($res,MYSQL_ASSOC)
However I will second that you should use PDO or mysqli instead
Use mysql_fetch_assoc
No, don't do that. Instead use PDO or mysqli and their respective fetch methods.
mysql_fetch_array fetches both numeric and associative arrays simultaneously.
mysql_fetch_array() as it's second paramter by default at "MYSQL_BOTH" meaning it return an array with both numerical and associative key.
To have only one of those, you can specify it in the call
mysql_fetch_array($res, MYSQL_ASSOC); // for assosiative
// OR
mysql_fetch_array($resm MYSQL_NUM); // for numeric
For more information you can take a look at the PHP documentation : http://php.net/manual/en/function.mysql-fetch-array.php
Try using PDO. The PDOStatement class fetch methods allow you to set the format of the returned data.
Here is some code for retrieving your data in an associative array:
try {
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$sth = $dbh->prepare("SELECT * FROM contas");
if($sth->execute()) {
$contacts = $sth->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($contacts);
} else {
throw new PDOException(print_r($sth->errorInfo(), true));
}
} catch(PDOException $e) {
echo $e->getMessage();
}