user3113732
user3113732

Reputation: 95

Fatal error: Call to a member function setFetchMode() on a non-object[Normal]

I'm facing following error from my AJAX called php script. Fatal error: Call to a member function setFetchMode() on a non-object. Actually I have written a Stored procedure to create dynamic temporary table. Please find below the Stored procedure and php script for call stored procedure.

`CREATE DEFINER=`root`@`localhost` PROCEDURE `StoreValue`(IN `Clname` VARCHAR(55), IN `ip_a` VARCHAR(55), IN `port_n` INT, IN `table_1` VARCHAR(55), IN `variable1` VARCHAR(55), IN `moddate1` TIMESTAMP, IN `moddate2` TIMESTAMP)
BEGIN
DECLARE table_name VARCHAR(55);
SET @table_name := table_1;

set @sql_text:=concat('create temporary table ',@table_name,'(status varchar(100),moddate datetime,INDEX mod_index (moddate)) '," SELECT `status`,`moddate` FROM `globstats_graph` WHERE Cname=? and ip=? and port=? and Variable=? and status REGEXP '^[0-9]+$|^ON$' and moddate between ? and ?");  

PREPARE stmt FROM @sql_text;
set @a=Clname;
set @b=ip_a;
set @c=port_n;
set @d=variable1;
set @e=moddate1;
set @f=moddate2;
 EXECUTE stmt USING @a,@b,@c,@d,@e,@f;
END`

php code: for connection

try {
            $conn = new PDO("mysql:host=$hostname;dbname=$database",$username, $password);
} 
catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}

for call stored procedure

try {
            // execute the stored procedure
            $sql = "CALL StoreValue('$client','$ip','$port','$table_name','$var','$new_start','$new_end')";
            $q = $conn->query($sql);
            $q->setFetchMode(PDO::FETCH_ASSOC);
            $total_search=$q->rowCount();
        } catch (PDOException $pe) {

            echo die("Error occurred:" . $pe->getMessage());
        }

Please help me out from this problem...

Upvotes: 0

Views: 9567

Answers (1)

Cristik
Cristik

Reputation: 32853

Your $q variable is not an object, this causes the fatal error. Most likely the $conn->query() method call returned the problematic value, and you will need to dig a little bit to find out why the query() method failed.

One of the best places to start searching is the PDO documentation to see what support it has for error reporting. Particularly, I think you'll find this user comment very interesting, considering the fact that you wrapped the query code into a try/catch block: http://php.net/manual/en/pdo.query.php#74943.

The handling of errors by this function is controlled by the attribute PDO::ATTR_ERRMODE.

Use the following to make it throw an exception:
<?php $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); ?>

Upvotes: 1

Related Questions