致命错误:在非对象上调用成员函数setFetchMode()[Normal]

时间:2015-04-05 12:59:14

标签: php mysql stored-procedures

我正面临来自我的AJAX的错误,称为php脚本。致命错误:在非对象上调用成员函数setFetchMode()。实际上我已经编写了一个存储过程来创建动态临时表。请在下面找到存储过程和php脚本的调用存储过程。

`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代码: 用于连接

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

用于调用存储过程

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());
        }

请帮我解决这个问题...

1 个答案:

答案 0 :(得分:0)

您的$ q变量不是对象,这会导致致命错误。很可能$ conn-> query()方法调用返回了有问题的值,您需要稍微挖掘一下以查找query()方法失败的原因。开始搜索的最佳位置之一是PDO文档,以查看它对错误报告的支持。特别是,考虑到您将查询代码包装到try / catch块中,我认为您会发现此用户评论非常有趣:http://php.net/manual/en/pdo.query.php#74943