2012-06-29 91 views
-5

我有php脚本的问题。 问题是,它显示我,我正在调用一个非对象的函数,但该对象存在。调用一个非对象,实际上是一个对象

脚本是:

if ($dcdt_sql['pdo']) { 
try { 
    $dbh = new PDO(
     'mysql:host='.$dcdt_sql[0].';dbname='.$dcdt_sql[3], 
     $dcdt_sql[1], 
     $dcdt_sql[2], 
     array(
      PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", 
      PDO::ATTR_PERSISTENT => false 
     ) 
    ); 
} 
catch (PDOException $e) { die("PDO ERR: [".$e->getMessage()."]"); } 
} 
else { $dbh = DBManager::connect(); } 

switch ($mode) { 
case 'fetch_assoc': 
    if ($dcdt_sql['pdo']) { 
     try { 
      $sth = $dbh->prepare($sqlQuery)->execute(); 
      $result = $sth->fetchAll(PDO::FETCH_ASSOC); // PROBLEM IS IN THIS LiNE 
      $return = $result; 
     } 
     catch (PDOException $e) { die("PDO ERR: [".$e->getMessage()."]"); } 
    } 
    else { 
     $result = $dbh->query($sqlQuery); 
        if (!is_object($result)) { die('DEBUG: Query error: ['.$sqlQuery.'] returned: ['.print_r($result,1).']'); }//DEBUG 
     while ($row = $result->fetch_assoc()) { 
      $list[] = $row; 
     } 
     $return = $list; 
    } 
break; 

问题出在哪里我评论它,但它应该是一个对象卫生组织函数被调用。 所以我不明白。

错误,我得到:

致命错误:调用非对象/usr/local/www/apache22/data/centrs/dc_elec/report.lib.inc上一个成员函数使用fetchall() 102行

谢谢你提前。

+2

http://sg.php.net/manual/en/pdostatement.execute.php'execute()'返回一个布尔值,以便错误消息是正确的。 –

+1

这就是你得到的方法链接。不要这样做,否则你将无法轻松发现这样的错误。 – PeeHaa

+4

口译员不说谎 – Gordon

回答

2

http://sg.php.net/manual/en/pdostatement.fetchall.php

正确使用所决定:

<?php 
$sth = $dbh->prepare("SELECT name, colour FROM fruit"); 
$sth->execute(); 

/* Fetch all of the remaining rows in the result set */ 
print("Fetch all of the remaining rows in the result set:\n"); 
$result = $sth->fetchAll(); 
print_r($result); 
+0

感谢您的快速回复。 – dpitkevics

0

也许尝试

$sth = $dbh->prepare($sqlQuery); 
$sth->execute(); 
$result = $sth->fetchAll(PDO::FETCH_ASSOC); // PROBLEM IS IN THIS LiNE 
$return = $result; 
1

execute方法返回TRUE或FALSE,而不是对象。尝试

$sth = $dbh->prepare($sqlQuery); 
$sth->execute(); 
$result = $sth->fetchAll(PDO::FETCH_ASSOC); 
0

尝试:

 $sth = $dbh->prepare($sqlQuery); 
     $sth->execute(); 
     $result = $sth->fetchAll(PDO::FETCH_ASSOC); // PROBLEM IS IN THIS LiNE 
     $return = $result; 

这应该工作,如执行会返回一个布尔值,而不是对象本身。对其他代码进行批评;当你使用对象时,请查看多态性,如果将“脏工作”放入专用对象中,那么if/else语句不应该是必需的。

相关问题