[RESOLVED] PDO connection caching !!!
hi
I have mad my website locally and it works fine, but when I upload it I get this error message:
Fatal error: Call to a member function fetch() on a non-object in /home/rdisast/public_html/class.php on line 63
for debugging, I have changed the password of db connection to a wrong password, but I didn't get "unable to connect" message, the same error appears
is it possible that this is cache or something else?
Re: PDO connection caching !!!
I think, your query might have failed and have returned a FALSE, instead of the resultset. So, you might have been trying to fetch rows from this without checking whether the query have returned a resultset or a FALSE value.
Example:
Code:
$r = $db->query("SELECT something FROM shelf");
if( $r === false )
{
echo 'Sorry, the query failed!';
}
else
{
// fetch rows...
}
:wave:
Re: PDO connection caching !!!
thanks for replay akhileshbc
yes, it returns false, but why does it return false while I have tested the sql statement at phpMyAdmin and it works !!!
here is my code :
PHP Code:
<?php
class myClass
{
public $dbh;
public function __construct()
{
$this->dbh = new PDO('mysql:host=localhost;rdisast_db', 'rdisast_user', '$+f~65G@e');
}
public function getFieldData($tableName, $fieldNumber, $value, $columnName)
{
$sth = $this->dbh->query("SELECT * FROM `$tableName` WHERE `$columnName`='$value'");
$fetch = $sth->fetch();
return $fetch[$fieldNumber];
}
public function preLang($httpReferer)
{
$l = '';
$explodedReferer = explode('/',$httpReferer);
$c = count($explodedReferer);
for ($i = 0; $i < $c; $i++) {
if ($explodedReferer[$i] == 'du' || $explodedReferer[$i] == 'en') {
$l = $explodedReferer[$i];
break;
}
}
if ($l == '') {
$l = $this->getFieldData('settings','dl','1','id');
}
return $l;
}
}
$obj = new myClass;
if (!$_GET['lang']) {
$language = $obj->preLang($_SERVER['HTTP_REFERER']);
} else {
$language = $_GET['lang'];
}
if ($language == 'du') {
$lKey = 2;
} else {
$lKey = 1;
}
$tr = array();
$gT = $obj->dbh->query('SELECT * FROM `translate`');
while($t = $gT->fetch()) {
$tr[$t['code']] = $t['word'.$lKey];
}
?>
the first execution of a sql statemnt is in getFieldData() function, and when I debricated it, the error become at the line of while loop "while($t = $gT->fetch()) "
I think the problem in in db connection, but I am sure of the username and password, and permissions for this user
Re: PDO connection caching !!!
try
PHP Code:
print_r($obj->$dbh->errorInfo())
Re: PDO connection caching !!!
thank you penagate
oh !!!!
Fatal error: Cannot access empty property in /home/rdisast/public_html/class.php on line 260
Re: PDO connection caching !!!
What's line 260?
Put that code after the query that fails.
For example:
PHP Code:
$sth = $this->dbh->query("SELECT * FROM `$tableName` WHERE `$columnName`='$value'");
if ($sth === null)
print_r($this->dbh->errorInfo());
Re: PDO connection caching !!!
ok :),
this is the result
Array ( [0] => 3D000 [1] => 1046 [2] => No database selected )
the line 260 is located in getFieldData() which is $fetch = $sth->fetch();
solved: error syntax of creating PDO connection.
thank you guys :) :)