This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
How can I loop this table in PHP?
$query= "SELECT * FROM table1";
$select= mysqli_query($connection, $query);
$row = mysqli_fetch_array($select);
while ($row = mysqli_fetch_array("$select")) // line 21
{
echo $row["column1"];
}
I have updated the code and I get this error
Recoverable fatal error
: Object of class mysqli_result could not be converted to string on line 21
There are multiple issues with your current code.
You fetch the first row with $row = mysqli_fetch_array($select); (line 3), but you don't do anything with it. This means that the first result is discarded.
Your while loop attempts to loop over an incorrect variable ($query is a string, not the result-object), and you've quoted it into a string - you need to do it as you were with the first fetch (line 3).
You don't do anything inside your loop, so the results aren't printed. At the very least, you should print them with echo.
$query = "SELECT * FROM table1";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($result)) {
echo $row["column1"]."<br />\n";
}
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 answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
There is some data is stored in my sql table. I want to fetch the data in the form of string.
$sql= "SELECT hash FROM signupinfo WHERE fname = 'nikhil';";
$hashfix = mysqli_query($connection,$sql);
echo $hashfix;
but the error coming
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\WampDeveloper\Websites\localhost\webroot\signup.php on line 48
var_dump($hashfix). If you want to read your array all Data fetched from a db come in the form of array
Its not a good idea to echo array...by that I mean you can't. What you can do in to make a string is. $a=implode(',',$hashfix)
There are 2 ways to print values you want
If its a single key array.
echo $hashfix[0]->ColumnName
Or run a foreach
You could do something like this...
$sql= "SELECT `hash` FROM `signupinfo` WHERE `fname` = 'nikhil';";
$hashfix = mysqli_query( $connection, $sql );
if( $hashfix ){
while( $rs=$connection->fetch_object( $hashfix ) ){
echo $rs->hash;
}
}
This question already has an answer here:
PDO looping through and printing fetchAll
(1 answer)
Closed 7 years ago.
I did a query in php and all the results are stored in an object. For example
$query = "select * from some_table";
$sth = $dbc->query($query);
$results = $sth->fetchAll();
Therefore the results of the query is stored in $results and I have a property called name. How can I now loop through this object $results to change all the values of a certain property name of the object.
Assuming name is a public property:
for($i = 0; $i < count($results); $i++){
$results[$i]->name = ...;
}
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\xxx\dash.php on line 20
I am quite fairly new, and being a old-school coder, simply using mysql_result to grab such data, I am unaware of how to go about this. I have a class->function setup.
Line 20 of dash.php contains:
echo $user->GetVar('rank', 'Liam', $mysqli);
While, the function is:
function GetVar($var, $username, $mysqli)
{
$result = $mysqli->query("SELECT " . $var . " FROM users WHERE username = '" . $username . "' LIMIT 1");
return $result;
$result->close();
}
Now, to my understanding, I am meant to convert $result into a string, but I am not fully aware of how to do so. I've tried using a few methods, but to no avail. So I've come to the community to hopefully get a answer, I've also looked around but noticed that all other threads are asking for num_rows, while I just want to grab the string from the query select.
You have to fetch it first before echoing the results. Rough Example:
function GetVar($var, $username, $mysqli) {
// make the query
$query = $mysqli->query("SELECT ".$var." FROM users WHERE username = '".$username."' LIMIT 1");
$result = $query->fetch_assoc(); // fetch it first
return $result[$var];
}
Then use your function:
echo $user->GetVar('rank', 'Liam', $mysqli);
Important Note: Since you're starting out, kindly check about prepared statements. Don't directly append user input on your query.
if ($result = $mysqli->query($query)) {
while($row = $result->fetch_object()) {
echo row['column_name'];
}
}
$result->close();
where you see 'column_name put the name of the column you want to get the string from.