msqli_result could not be converted to string [duplicate] - php

This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
I'm trying to query the DB with mysqli and then fetch the result, but it's throwing an Object of class could not be converted to a string error.
Here's what I am trying to accomplish:
<?php
include ('conn.php');
$query= "select value from the_table where item = 'url'";
$result = mysqli_query($conn, $query);
?>
And then I am trying to populate a link with the $result:
This is the Link to the URL
I saw this post: PHP and MySQL error: Object of class mysqli_result could not be converted to string
So, I tried to format the echo with echo $result->fetch_object()->url;, but that didn't work.
I'm not sure if I have to fetch the result and then throw it into a look with a mysqli_fetch_array() and if so, how do I get it to populate that the value outside of the loop?

Assuming that $db is your database connection link you can perform the query:
$result = mysqli_query($db, 'select name from fruits');
Then you can get name using procedural style:
echo mysqli_fetch_object($result)->name;
Or object style:
echo $result->fetch_object()->name;
In your code you're trying to output url property which is not defined - as we can see in your query.

Try some thing like this:-
$query= "select value from the_table where item = 'url'";
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
// use returned data
while($row = mysqli_fetch_assoc($result))
{
echo $row['value'];
}
OR
while ( $row = mysqli_fetch_assoc( $result ) )
{
foreach ($row as $key => $value)
{
print ($row . " = " . $value . "\n");
}
print("================");
}

Related

How to convert an object of class mysqli_result to a json object

With the php code below, I am trying to select a record from my database. When I run the code I get this error:
Catchable fatal error: Object of class mysqli_result could not be
converted to string
What I want to achieve is to convert the result into a jSON object, but instead I get this error.
<?php
session_start();
include_once 'db/dbconnect.php';
$var = $_GET['name'];
// echo $var;
$json = [];
$sql = "SELECT * from recipes WHERE recipes.recipeName = '.$var'";
$rslt = mysqli_query($con,$sql);
echo $rslt;
?>
you will need to iterate over the results, since mysqli returns one row at a time:
$sql = "SELECT * from recipes WHERE recipes.recipeName = '$var'";
$rslt = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($rslt)){
print_r($row);
}
or, to JSON it:
$json = array();
while($row = mysqli_fetch_assoc($rslt)){
$json[] = $row;
}
echo json_encode($json);
mysqli_fetch_assoc returns the row as a keyd array - http://php.net/manual/en/mysqli-result.fetch-assoc.php
as to SQL injection defence, use mysqli_real_escape_string (http://php.net/manual/en/mysqli.real-escape-string.php) like:
$var = mysqli_real_escape_string($con,$_GET['name']);

SQL select as a PHP function [duplicate]

This question already has answers here:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
(3 answers)
Closed 7 years ago.
I'm selecting data from MySQL. My code looks like this:
$sql = "SELECT oznacenie_odpadu FROM zdroj_dat ORDER by ID ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['oznacenie_odpadu'].'" >'.$row['oznacenie_odpadu'].'</option>';
}
}
This part of code is in my code multiple times. I'm calling it 5times only string "oznacenie_odpadu" is changing. Therefore I made function:
function select($data) {
$sql = "SELECT rozmer FROM zdroj_dat ORDER by id ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row[$data].'" >'.$row[$data].'</option>';
}
}
}
Calling it with select("somevalue");
Syntax is ok because I didn't change anything but when I load the page the data from database are not retrieved.
You have a variable scope issue. $conn is not available inside of your function. You need to pass it as a parameter so ic an be used inside of your function.
function select($conn, $data) {
$sql = "SELECT rozmer FROM zdroj_dat ORDER by id ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row[$data].'" >'.$row[$data].'</option>';
}
}
}
Call it:
select($conn, "somevalue");

PHP MYSQLi: Array to string conversion [duplicate]

This question already has answers here:
Notice: Array to string conversion in
(6 answers)
Closed 7 years ago.
I want to get the id of a table and make it a variable so I can use it in a link.
But I am getting Notice: Array to string conversion in .
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error());
$pid = mysqli_fetch_assoc($res);
<a id='del' href='deletepost.php?del=$pid'>Delete</a>
This is just a part of the code where I have problems.
You need to do like this:-
<?php
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error($conn));
while($pid = mysqli_fetch_assoc($res)){
echo "<a id='del' href='deletepost.php?del=$pid['id']'>Delete</a><br/>";
}
Note:- this is because $res is an result-set array object having 1 or more than value. So you need to iterate like this.
$pid is an array, you need to access the ID from the array then you can use it in the link.
Example:
$id = $pid['id'];
<a id="del" href="deletepost.php?del=$id">Delete</a>

Object of class mysqli_result could not be converted to string on mybb [duplicate]

I am getting the error:
Object of class mysqli_result could not be converted to string
This is my code:
$result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'");
echo "my result <a href='data/$result.php'>My account</a>";
The mysqli_query() method returns an object resource to your $result variable, not a string.
You need to loop it up and then access the records. You just can't directly use it as your $result variable.
while ($row = $result->fetch_assoc()) {
echo $row['classtype']."<br>";
}
Before using the $result variable, you should use $row = mysqli_fetch_array($result) or mysqli_fetch_assoc() functions.
Like this:
$row = mysqli_fetch_array($result);
and use the $row array as you need.
mysqli:query() returns a mysqli_result object, which cannot be serialized into a string.
You need to fetch the results from the object. Here's how to do it.
If you need a single value.
Fetch a single row from the result and then access column index 0 or using an associative key. Use the null-coalescing operator in case no rows are present in the result.
$result = $con->query($tourquery); // or mysqli_query($con, $tourquery);
$tourresult = $result->fetch_array()[0] ?? '';
// OR
$tourresult = $result->fetch_array()['roomprice'] ?? '';
echo '<strong>Per room amount: </strong>'.$tourresult;
If you need multiple values.
Use foreach loop to iterate over the result and fetch each row one by one. You can access each column using the column name as an array index.
$result = $con->query($tourquery); // or mysqli_query($con, $tourquery);
foreach($result as $row) {
echo '<strong>Per room amount: </strong>'.$row['roomprice'];
}
The query() function returns an object, you'll want fetch a record from what's returned from that function. Look at the examples on this page to learn how to print data from mysql
Try with:
$row = mysqli_fetch_assoc($result);
echo "my result <a href='data/" . htmlentities($row['classtype'], ENT_QUOTES, 'UTF-8') . ".php'>My account</a>";

Object of class mysqli_result could not be converted to string

I am getting the error:
Object of class mysqli_result could not be converted to string
This is my code:
$result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'");
echo "my result <a href='data/$result.php'>My account</a>";
The mysqli_query() method returns an object resource to your $result variable, not a string.
You need to loop it up and then access the records. You just can't directly use it as your $result variable.
while ($row = $result->fetch_assoc()) {
echo $row['classtype']."<br>";
}
Before using the $result variable, you should use $row = mysqli_fetch_array($result) or mysqli_fetch_assoc() functions.
Like this:
$row = mysqli_fetch_array($result);
and use the $row array as you need.
mysqli:query() returns a mysqli_result object, which cannot be serialized into a string.
You need to fetch the results from the object. Here's how to do it.
If you need a single value.
Fetch a single row from the result and then access column index 0 or using an associative key. Use the null-coalescing operator in case no rows are present in the result.
$result = $con->query($tourquery); // or mysqli_query($con, $tourquery);
$tourresult = $result->fetch_array()[0] ?? '';
// OR
$tourresult = $result->fetch_array()['roomprice'] ?? '';
echo '<strong>Per room amount: </strong>'.$tourresult;
If you need multiple values.
Use foreach loop to iterate over the result and fetch each row one by one. You can access each column using the column name as an array index.
$result = $con->query($tourquery); // or mysqli_query($con, $tourquery);
foreach($result as $row) {
echo '<strong>Per room amount: </strong>'.$row['roomprice'];
}
The query() function returns an object, you'll want fetch a record from what's returned from that function. Look at the examples on this page to learn how to print data from mysql
Try with:
$row = mysqli_fetch_assoc($result);
echo "my result <a href='data/" . htmlentities($row['classtype'], ENT_QUOTES, 'UTF-8') . ".php'>My account</a>";

Categories