带有消息语法错误或访问冲突的未捕获的PDO异常:1064

时间:2016-10-04 18:14:26

标签: php pdo

这是我的dboperations.php片段:

class DB{
    private $host="localhost";
private $db_name="abcd";
private $db_user="root";
private $db_pass="";

public function __construct(){
    if(!isset($this->db)){
        try{
            $conn=new PDO("mysql:host=".$this->host.";dbname=".$this->db_name,$this->db_user,$this->db_pass);
            $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            $this->db=$conn;
        }
        catch(PDOException $e){
            die("Failed to connect with MySQL: ".$e->getMessage());
        }
    }
}

public function getRows($table,$conditions=array()){
    $sql='SELECT ';
    $sql.=array_key_exists("select",$conditions)?$conditions['select']:'*';
    $sql.=' FROM '.$table;
    if(array_key_exists("where",$conditions)){
        $sql.=' WHERE ';
        $i=0;
        foreach($conditions['where'] as $key=>$value){
            $pre=($i>0)?' AND ':'';
            $sql.=$pre.$key." = '".$value."'";
            $i++;
        }
    }
    if(array_key_exists("order_by",$conditions)){
        $sql .= ' ORDER BY '.$conditions['order_by']; 
    }

    if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
        $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit']; 
    }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
        $sql .= ' LIMIT '.$conditions['limit']; 
    }

    $query = $this->db->prepare($sql);
    $query->execute();

    if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
        switch($conditions['return_type']){
            case 'count':
                $data = $query->rowCount();
                break;
            case 'single':
                $data = $query->fetch(PDO::FETCH_ASSOC);
                break;
            default:
                $data = '';
        }
    }else{
        if($query->rowCount() > 0){
            $data = $query->fetchAll();
        }
    }
    return !empty($data)?$data:false;
    }
}

这是我的index.php片段:

<?php
    include 'DBOperations.php';
                $db=new DB();
                $members=$db->getRows('members',array('where'=>array('team_id'=>$_SESSION['team_id']),'order_by'=>'user_id DESC'));
                if(!empty($members)){
                    $count=0;
                    foreach($members as $member){
                        $user_id=$member['user_id'];
                        $user=$db->getRows('users',array('where'=>array('index'=>$user_id)));?>

我在运行上面的代码时遇到问题标题中提到的错误。我在index.php的最后一行得到错误。请告诉我如何解决这个问题。你可以看到我有两个表在我的数据库中:users和members.members有3列:index,user_id,team_id.users有几列:index,first_name,last_name,email,phone等。为什么我收到此错误。请帮我解决这个问题。

0 个答案:

没有答案