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.
Related
Since moving to PHP 8 the code below now gives me the error 'Fatal error: Uncaught Error: Non-static method mysqli_result::fetch_array() cannot be called statically...'
if (!isset($isone)) {
$seestuno=isset($_POST['seestuno']);
}
if (isset($seestuno)) {
include ('/home/username/private/dblon.php'); //database connection ($dbcnx below)
$sql="SELECT stuno,fname,lname FROM Studetails WHERE stuno=$seestuno";
$result=#mysqli_query($dbcnx, $sql);
$row=#mysqli_result::fetch_array($result); //I changed from mysql_fetch_array. I believe this is more correct?
The bottom line gives the error. I thought this was basic stuff (since I did it) so can anyone help me out? Many thanks.
mysqli_result::fetch_array ( int $mode = MYSQLI_BOTH ) is the method signature for the method "fetch_array" of the mysqli_result class. Therefore, you'd have to call it as an object method:
$row = $result->fetch_array();
Or, of course, you use the equivalent to mysql_fetch_array instead:
$row = mysqli_fetch_array($result);
There are examples at the bottom of the official docs for that. The rest of your code uses mysqli's procedural interface, so I'd stick to the later case unless all mysqli calls are converted to object oriented style.
:: is a scope resolution operator and is used to access static and constant members of a class.
fetch_array() is not a static method. You can't access it statically. It is an instance method that can be called on every object.
To call a method on an object you have to use object operator ->.
When you fix your code, it should look something like this:
if (!isset($isone)) {
$seestuno = isset($_POST['seestuno']);
}
if (isset($seestuno)) {
include '/home/username/private/dblon.php'; //database connection ($dbcnx below)
$stmt = $dbcnx->prepare("SELECT stuno,fname,lname FROM Studetails WHERE stuno=?");
$stmt->bind_param('s', $seestuno);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array($result);
}
I have cerb2 https://github.com/wgm/cerb2, it is an old ticketing system, it might looks weird but I have a a lot of knowledgeable information burried that I want to exploit.
It is basically a PHP5/Mysql software with the mysql_connect() constructor
The main issues are queries that stay null at all times. The code base lays on Class definition and pseudo-variable $this to call a query method from another Class.
function CER_HASH_QUEUES(&$parent)
{
global $session;
global $queue_access; e();
$this->db = (new cer_Database())->getInstance();
$this->_parent = &$parent;
if(empty($queue_access))
$this->queue_access = new CER_QUEUE_ACCESS();
else
$this->queue_access = $queue_access;
$sql = "SELECT q.queue_id, q.queue_name FROM queue q ORDER BY q.queue_name";
$res = $this->_db->query($sql);
That last line ($res =… ) is stopping code execution with:
Fatal error: Call to a member function query() on null in
The Query method called from cer_Database.class looks like this ( https://raw.githubusercontent.com/wgm/cerb2/stable/cerberus-api/database/cer_Database.class.php )
function query($sqlString,$return_assoc=true)
{
$config_db = (new cerConfiguration())->getInstance();
if($return_assoc === true) $this->db->SetFetchMode(ADODB_FETCH_ASSOC);
else $this->db->SetFetchMode(ADODB_FETCH_NUM);
$res = $this->db->Execute($sqlString);
if($cfg->settings["debug_mode"]) {
$time_end = microtime();
$query_time = microtime_diff($time_end,$time_start) * 1000; // convert secs to millisecs
echo "<b>[CERBERUS QUERY]:</b> " . $sqlString . " (Ran: <b>" . sprintf("%0.3f",$query_time) . "ms</b>)<hr>";
}
return $res;
}
That last method depends on a second Class cerConfiguration() which relies on import_config_db() to construct the mysql connector.
I am unable to figure out how to pass successfully my sql request following the design pattern carried by the relevant software here.
The databases and the tables are OK on their sides, and the following php script is returning the sql query OK.
<?php
include('includes/third_party/adodb/adodb.inc.php');
$db = ADONewConnection('mysql');
$db->Connect("localhost", "user", "pass", "databasename");
$rs = $db->Execute('SELECT q.queue_id, q.queue_name FROM queue q ORDER BY q.queue_name');
print "<pre>";
print_r($rs->GetRows());
print "</pre>";
I guess without the object oriented design pattern understanding used by PHP I am going to struggle to resuscitate that old piece of software.
Any help, ideas very welcome.
Yours.
Fatal error: Call to a member function query() on null in
You are likely to find further issues in your investigation, but the problem you're asking for help with is relatively simple.
The database property in the class is defined early on in your code as $this->db, but you are then trying to access it as $this->_db. The underscore makes it a different variable name, and thus it doesn't exist. So it is null, hence why you are getting Call to a member function query() on null.
To fix it, make sure you're using the same name for it throughout the class.
This question already has answers here:
Catchable fatal error: Object of class PDOStatement could not be converted to string in line 114 [duplicate]
(3 answers)
Closed 3 years ago.
The code I'm doing maintenance is a school system, I'm having problems when it arrives in the part of starting the enrollment, it returns an object error, that is in line 111, in my code this is giving error in line 111 in
$qryMatricula = $conn->query($sqlMatricula);
echo $qryMatricula;
What could I be? How can I solve this?
main.loader.php
<?php
$sqlMatricula = "SELECT M.Situacao FROM {$pfx}Matricula M
INNER JOIN {$pfx}Curso C ON (C.CursoID = M.CursoID)
WHERE AlunoID = $alunoid AND C.PeriodoLetID = $periodoid";
$qryMatricula = $conn->query($sqlMatricula);
echo $qryMatricula;
$situacaoMatricula = 0;
if ($m = $qryMatricula->fetchObject())
$situacaoMatricula = $m->Situacao;
I guess you are using PDO::query() which returns an instance of a PDOStatement object and could not be converted to a string with echo.
Try the following:
$sth = $conn->query($sqlMatricula);
$result = $sth->fetchAll();
var_dump($result);
$SourceID = $this->source_information->SourceID;
// the following fails
if($results = $this->mysqli->query("SELECT .... R.Name = '$release_name' AND S.SourceID = $this->source_information->SourceID AND S.ReleaseID = R.ReleaseID"))
// this will works
if($results = $this->mysqli->query("SELECT .... R.Name = '$release_name' AND S.SourceID = $SourceID AND S.ReleaseID = R.ReleaseID"))
I have a ton of code with $this->source_information->SourceID sort if thing in it and I really don't what to rewrite it, tell me I can make this work some how.
edits follow:
exit(var_dump($this->source_information->SourceID));
returns(string(2) "18")
Thank you for bringing up prepared statements. I will use prepared statements from now on.
Simple variable interpolation syntax, i.e. "$this->foo", will only resolve a maximum of one nested object. "$this->foo->bar" is interpreted as $this->foo plus the string "->bar". Which is why it's complaining about the source_information object. If you want to embed deeper nested objects, use the complex variable interpolation syntax:
"... S.SourceID = {$this->source_information->SourceID} AND ..."
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();