I've tried the following script in php:
$prod_no = " SELECT no FROM e_produit WHERE nom LIKE '%NAVIGATOR%' ";
$stmt = $pdo->query($prod_no);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row;
But I get the error:
Catchable fatal error: Object of class PDOStatement could not be converted to string.
Object of class PDOStatement could not be converted to string.
Meaning, you cannot echo $row, because $row isn't a string.
Debug:
print_r($row, true)
Nice print:
echo $row['no'];
From the manual
"PDO::FETCH_ASSOC: Return next row as an array indexed by column name"
Related
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('censored','censored','censored','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
$sql="SELECT message FROM messages WHERE code = '".$q."'";
$result = mysqli_query($con,$sql);
$resultstring = (string)$result;
echo $resultstring;
mysqli_close($con);
?>
I am attempting to echo the result of the query to the user but when this PHP runs through ajax I get this error:
Recoverable fatal error: Object of class mysqli_result could not be
converted to string in D:\xampp\htdocs\getmessage.php on line 12
Now I don't understand this because I am already converting $result into a string.. Thanks!
mysqli_query() can not convert to string.
You must use mysqli_fetch for parse to array.
Example:
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
printf ("%s (%s)\n",$row[0],$row[1]);
}
}
First of all mysqli_query Performs a query on the database
it does not directly return a value
$query = mysqli_query($con,$sql);
so to do that use mysqli_fetch_assoc it will Fetch a result row as an associative array
$result = mysqli_fetch_assoc($query);
Then you can now get your value
$resultstring = $result['message'];
so your code should be like this
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('censored','censored','censored','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
$sql="SELECT message FROM messages WHERE code = '".$q."'";
$query = mysqli_query($con,$sql);
$result = mysqli_fetch_assoc($query);
$resultstring = $result['message'];
echo $resultstring;
mysqli_close($con);
?>
you need to convert the data(RS) into array !
$result = mysqli_fetch_assoc($result);
print_r($result);
As an result of your query on success You will get mysqli_result object. If You want to get received rows use fetch data function.
$result = mysqli_query($con,$sql);
$resultstring = $result->fetch_row()[0];
See docs: mysqli_query &
mysqli_result
I have resolved similar issue with following approach.
// Assign your sql statement
$sqlMasterSiteIds = "SELECT DISTINCT table_name FROM site WHERE gwid = 39 AND master_site_id NOT LIKE '0'";
// Run the query
$masterSiteIdsQuery = $this->db->query($sqlMasterSiteIds);
// Prepare container
$masterSiteIds = [];
// Fetch first row of query results
$first_row = $masterSiteIdsQuery->fetch_assoc();
// Assign first row to your prepared container
$masterSiteIds= $first_row['master_site_id'];
// Change the results into string comma separated so it can be used in next mysqli query or echoed
while($sub_row = $masterSiteIdsQuery->fetch_assoc()) {
$masterSiteIds = implode(',', array($masterSiteIds, $sub_row['master_site_id']));
}
// Now you can use $masterSiteIds which is a STRING of values comma separated
echo $masterSiteIds; // Output: '1024, 1142, 8492'
I hope it will help you or anybody else in need. I am not sure if it is the best approach but it worked well in my case as I had to change my results into STRING so I could use it in my next mysqli query with FIND_IN_SET command.
Happy coding!
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.
I am getting error while running following code
$value_sql = "SELECT test_fielf FROM `tbl_test` where `site_id`='".$sid."'";
$register_value = $db->fetchRow($value_sql);
echo $register_value;die();
The error : Catchable fatal error: Object of class stdClass could not be converted to string
you cannot use echo for printing objects
$value_sql = "SELECT test_fielf FROM `tbl_test` where `site_id`='".$sid."'";
$register_value = $db->fetchRow($value_sql);
print_r( $register_value);
die();
Assuming the code you posted is as-is, you're missing a closing double-quote on the first line.
$value_sql = "SELECT test_fielf FROM `tbl_test` where `site_id`='".$sid."'";
$register_value = $db->fetchRow($get_register_value_sql);
echo $register_value;die();
The error you're experiencing is due to $register_value = $db->fetchRow($get_register_value_sql); returning an object, not a string. If you wish to treat is as a string, then you can cast is as a string using:
$register_value = (string) $db->fetchRow($get_register_value_sql);
Try this:
$value_sql = "SELECT test_fielf FROM `tbl_test` where `site_id`='" . $sid . "'";
$register_value = $value_sql -> fetchRow(DB_FETCHMODE_ASSOC);
print_r($register_value);
die();
This code works, and outputs an array as intended, but throws the following error message:
PHP Notice: Trying to get property of non-object...
Here is my code:
$mysqli = new mysqli("localhost","user","pass","database");
$sql = "SELECT keyterm
FROM keyterms";
$results = $mysqli->query($sql);
while($result = $results->fetch_object()->keyterm)
$keyterms[] = $result;
fetch_object() will return null when there are no more results in your result set, so the last iteration of the while loop will attempt to access the keyterm property on a null value.
You should assign the result object to $result and then access the property in the loop body, e.g.:
while($result = $results->fetch_object())
$keyterms[] = $result->keyterm;
$keyterms = array();
if ($results = $mysqli->query($sql))
{
while($obj = $results->fetch_object())
{
$keyterms[] = $obj->keyterm;
}
}
You get that warning because you look up the property before checking if the cursor returns a valid object. In other words on the last iteration when the recordset has been exhausted, you will have an empty object with no keyterm property.
The second echo below throws the error "Trying to get property of non-object?" The first echo prints fine.
$sql = "SELECT id
FROM files
WHERE url = '$url'";
$result = $this->mysqli->query($sql);
echo "companyId: " . $result->fetch_object()->id;
echo "\ncompanyId: " . $result->fetch_object()->id;
I've doubled up the echo for simplicity of demonstrating this error. I'm actually trying to check if $result->fetch_object()->id is set and then do something with the value if it is. I can't get that to work correctly because the second time i use it, it throws the error "trying to get property of non-object."
if(isset($result->fetch_object()->id))
echo $result->fetch_object()->id;
When you use fetch_object() the pointer moves to the next row, so the second time you call it you get the result of the next row or NULL if there are no more rows.
Take a look at the manual; code like this would result in an endless loop if the pointer would not move to the next row:
/* fetch object array */
while ($obj = $result->fetch_object()) {
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
}
Make a reference to it:
$result = $this->mysqli->query($sql);
$id = $result->fetch_object()->id;
echo "companyId: " . $id;
echo "\ncompanyId: " . $id;
The mysqli-resultsets works with cursors. This means, if you call fetch_object() the object is fetched (of course), the cursor moves on and now the previously retrieved object not present in the resultset anymore. Retrieve the object into a variable and interact with that, instead of calling fetch_object() multiple times.
$o = $result->fetch_object();