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();
Related
I'm trying to run a query; to make the string that has the query, I need to concatenate $org and $calendar . I have tried this:
$cal = $org.$year; //$org = 'COM', $year= '2021'
$condition = "coba.calendar LIKE '$cal%' "; //So it should be like "coba.calendar LIKE 'COM2021%'
$qry = "SELECT coba.field_1, coba.field_2
FROM table_1 coba
WHERE $condition";
And then run the $qry, but I'm getting a Fatal error: Allowed memory size of...
$rs = odbc_exec($link_ifx, $qry); //I'm getting a Fatal error: Allowed memory size of...
while ($row = odbc_fetch_array($ )) {
$array[] = $row;
}
echo json_encode($array);
But if I do this it works:
$qry = "SELECT coba.field_1, coba.field_2
FROM table_1 coba
WHERE coba.calendar LIKE 'COM2021%'";
I've found that I was using other string for $org, I was using 'BUR' originally instead of 'COM' as I thought I was.
coba.field_1 was CAST(substr(padt.paquete,4,length(padt.paquete)) AS INT) nu_paquete and while using it with $org = 'BUR' it didn't have NULLS but with 'COM' it had. And that was causing a Fatal error: Allowed memory.
I am trying to get an AJAX search working, i am very close to this. Here is the php that i am using.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Products";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$searchValue = $_GET['search'];
if(isset($searchValue) && $searchValue != ''){
$search = addslashes($searchValue);
$statement = $conn->prepare("SELECT ProductName FROM Product WHERE ProductName LIKE('" . $search . "%') ORDER BY ProductName");
$statement->execute();
$all = $statement->fetchAll(PDO::FETCH_ASSOC);
for($i=0; $i<count($all);$i++){
echo json_encode($all[$i]).ProductName;
}
}
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
The responseText i get is this:
Notice: Use of undefined constant ProductName - assumed 'ProductName'
in F:\xampp\htdocs\searchSuggest.php on line 23
{"ProductName":"iMac"}ProductName
Notice: Use of undefined constant ProductName - assumed 'ProductName'
in F:\xampp\htdocs\searchSuggest.php on line 23
The only thing i want to display is the "iMac" part of the json object
The fix
This line is incorrect:
echo json_encode($all[$i]).ProductName;
It looks like you are trying to get the productName as a property, but the operator for that is ->:
echo json_encode($all[$i])->ProductName;
That line is still incorrect though. The result of json_encode is not an object but a string. The right way to fix it, is to use the array result of fetchAll, and get the product name by the array key:
echo $all[$i]['ProductName'];
The error message
The . operator is for string concatination, so you are trying to concatinate to the json string, the constant ProductName, which is not defined. And that's exactly what the warning says: You are using the undefined constant ProductName, so PHP assumes you meant the constant string 'ProductName' instead.
With an -> it still won't work, though, since json_encode returns a string, not an object. You could json_decode it again, but that's a waste of processing time.
Possible solutions
You seem to be trying to treat the result as an object. Which would be done like this:
$all = $statement->fetchAll(PDO::FETCH_OBJ);
for($i=0; $i<count($all);$i++){
echo $all[$i]->ProductName;
}
or this:
while ($row = $statement->fetch(PDO::FETCH_OBJ)) {
echo $row->ProductName;
}
You probably want:
echo($all[$i]['ProductName']);
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"
I have found similar queries like mine in stackoverflow,but found no solutions . So I am asking it again. I have the following insert query :
$purchase_date = date("Y-m-d");
$init = substr($info[fname], 0, 2);
$odr = rand(0,255);
$invoice_number = $this->get_invoice_number();
//$invoice_number = $invoice_number+1;
//$invoice_number = 400 + rand(0,100);
$order_number = $init.'-'.$odr;
$session_id = session_id();
$sql = "
INSERT INTO
tbl_checkout
SET
fname = '$info[fname]',
lname = '$info[lname]',
email = '$info[email]',
phone = '$info[phone]',
address = '$info[address]',
pin = '$info[pin]',
session_id = '$session_id',
purchase_date = '$purchase_date',
invoice_number = '$invoice_number',
order_number = '$order_number' <----This is line no 1038
";
$this->db->insertQuery($sql);
But when I tried to execute it, it shows error like Catchable fatal error: Object of class stdClass could not be converted to string in c:\.....on line 1038
I am lost because I can't even understand what the error means ! Please help.
$this->get_invoice_number() is probably returning an object.
you can cast it to a string:
$invoice_number = (string) $this->get_invoice_number();
There is nothing wrong in line 1038,
most likely is line 1037 having an error
(valuable casting issue)
$invoice_number = $this->get_invoice_number();
You should do a
var_dump( $this->get_invoice_number() );
Then likely you just need to refer to the object property like
$invoice_number->SOME_PROPERTY;
I have a really odd error:
[04-Jun-2010 15:55:32] PHP Catchable
fatal error: Object of class Type
could not be converted to string in
/home/prettykl/public_html/2010/includes/functions.php
on line 140
This is the code and line 140 is the $sql line.
if (!empty($type)) {
$sql = "SELECT * FROM `types` WHERE `type` = '$type'";
$dbi = new db();
$result = $dbi->query($sql);
$row = mysql_fetch_row($result);
$dbi->closeLink();
$where_array[] = "`typeID` = '".$row->typeID."'";
$where_array[] = "`typeID2` = '".$row->typeID."'";
}
I have 5 or 6 classes and I've never encountered this problem before.
The function has no reference to the classes, any ideas??
Thanks,
Stefan
$type is an object of the class Type. You probably want to query a property of that object?
The error means that $type is actually an object (of class Type), so either the $type variable doesn't contain what you expect, or you actually want to get a member of the object instead, so $type->getSomeString(), etc.
This error happens when you try to convert an object to string and the object doesn't implement the __toString() method. PHP can't handle that conversion. For instance:
$s = new stdClass();
echo $s;
Catchable fatal error: Object of class stdClass could not be converted to
string in php shell code on line 1
Note that you are outputting $type inside the query, as if it was a string.