2013-06-21 40 views
-3

我有一个PDO的问题...
类,其中我有一个错误,用于执行选择(和未来 - 其他)查询数据库,使用PHP:PDO ......这样的:PDO - 调用一个非对象的成员函数execute()

$db = new PDOAct; 
    $arr = array("from" => "users"); 
    $row = array("id"); 
    $val = array(0); 
    $type = array("INT"); 
    $db->select($arr, $row, $val, $type); 

当我执行此,

<? if (!defined("sKEY")) { exit("Houston, We've Got a Problem"); } 
class PDOAct 
{ 
    public $db; 
    function __construct() 
    { 
     try { 
      $this->db = new PDO(DBdriver.':host='.DBhost.';dbname='.DBbase, DBuser, DBpass); 
      $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     } catch(PDOException $e) { 
      $this->err($e->getMessage()); 
     } 
    } 
    function select($arr, $row, $val, $type) 
    { 
     try { 
      for ($i=0; $i<count($row); $i++) 
      { 
       if (isset($row[$i]) && isset($val[$i]) && isset($type[$i])) 
       { 
        if ($arr[select] != "" && $arr[select] != "*") 
        { 
         if ($i < 1) 
         { 
          $do = $this->db->prepare("SELECT `".$arr[select]."` FROM `".$arr[from]."` WHERE `".$row[$i]."` = ':".$row[$i]."'"); 
         } else { 
          $do = $do.$this->db->prepare(" AND `".$row[$i]."` = ':".$row[$i]."'"); 
         } 
        } elseif ($arr[select] == "" || $arr[select] == "*") { 
         if ($i < 1) 
         { 
          $do = $this->db->prepare("SELECT * FROM `".$arr[from]."` WHERE `".$row[$i]."` = ':".$row[$i]."'"); 
         } else { 
          $do = $do.$this->db->prepare(" AND `".$row[$i]."` = ':".$row[$i]."'"); 
         } 
        } 
        $do->bindValue(':'.$row[$i], $val[$i], "PDO::PARAM_".$type[$i]); 
       } elseif (!isset($row[$i]) && !isset($val[$i]) && !isset($type[$i])) { 
        if ($arr[select] != "" && $arr[select] != "*" && $i == 0) 
        { 
         $do = $this->db->prepare("SELECT `".$arr[select]."` FROM `".$arr[from]."`"); 
        } elseif ($arr[select] == "" || $arr[select] == "*" && $i == 0) { 
         $do = $this->db->prepare("SELECT * FROM `".$arr[from]."`"); 
        } 
       } else { 
        exit("Query error!"); 
       } 
      } 
      var_dump($this->db->prepare("SELECT * FROM `".$arr[from]."` WHERE `".$row[0]."` = ':".$row[0]."'")); 
     } catch(PDOException $e) { 
      $this->err($e->getMessage()); 
     } 
     return $do; 
    } 
    function err($e) 
    { 
     file_put_contents('log'.DIR_SEP.'PDOerrors.txt', $e."\n", FILE_APPEND); 
     exit("Houston, We've Got a Problem"); 
    } 
} 
?> 

它给了我一个PHP错误:“调用一个成员函数执行( )在非物件上
我试图用var_dump(); - 它给了我不服这样的:

object(PDOStatement)#4 (1) { ["queryString"]=> string(40) "SELECT * FROM `users` WHERE `id` = ':id'" } 

所以$这个 - > DB->准备()是OK。 什么问题?我在这3个多小时里受到了这个痛苦..
谢谢!

+3

看起来像有没有执行()调用在那里... –

+0

是的。有var_dump(),但是如果我在这里放置$ do-> execute(),它会给我一个错误.. –

+1

除了上面的注释,你应该改变'$ arr [select]'到'$ arr ['select'] '等等。 – hjpotter92

回答

0

你不能做这样的东西:

if ($i < 1) 
{ 
    $do = $this->db->prepare("SELECT * FROM `".$arr[from]."` WHERE `".$row[$i]."` = ':".$row[$i]."'"); 
} else { 
    $do = $do.$this->db->prepare(" AND `".$row[$i]."` = ':".$row[$i]."'"); 
} 

你需要生成SQL语句(动态),只有当你有完整的账单准备好了,你prepare它,你不能连接,并准备部分语句。

+0

好吧..这就是为什么: '$ db = new PDOAct ; \t $ arr \t = array(“from”=>“users”); \t $ row \t = array(“id”,“login”); \t $ val \t = array(0,“Admin”); \t $ type \t = array(“INT”,“STR”); \t $ db-> select($ arr,$ row,$ val,$ type);' 不工作? –

+0

所以我需要生成sql查询并将其放入变量,然后准备一个变量? –

+0

@ZLOI_DED是的,你需要生成一个完整的查询字符串,当完成时,在执行之前使用'prepare'作为字符串。现在,您将连接对象,除了不能只准备“AND”条件。 – jeroen

0

要调用执行非对象,一定要使用正确的顺序

$data = array("id" => "your_row_id"); 
$STH = $this->db->prepare($your_query); // In which you are using your :id named placeholder 
$STH->execute($data); 
+0

现在我有一个其他的问题: 声明执行正常,但是当我这样做的时候: '$ db = new PDOAct; $ arr \t = array(“from”=>“users”); $ row \t = array(“id”,“login”); $ val \t = array(0,“Admin”); $ type \t = array(“INT”,“STR”); $ db-> select($ arr,$ row,$ val,$ type);' - 我得到一个500内部服务器错误... –

相关问题