asd123asd098qwe 2015-03-05 03:46 采纳率: 0%
浏览 1720

自己写的一个php的PDO的类,有点问题,求大神指点

自己写的一个php的PDO的类,在调用的时候可以查询,插入的时候没有报错,但是插入没有成功,这是怎么回事。。。。。。。。。。
这个是调用的类

 <?php 
header("content-type:text/html;charset=utf-8");
class dbPdoManger
{
    private $conn='';//连接数据库服务器的资源类型
    private $host="";//主机地址
    private $dbname="";//数据库名称
    private $user="";//数据库用户名
    private $pwd="";//密码
    private $charset="";//链接编码
    private $config=array();

    /*
     * 构造函数初始化数据库
     * 变量: $host连接的服务器名称
     *     $user登陆服务器的用户名
     *     $pwd登陆服务器的密码
     */
    public function __construct($config)
    {
        $this->config=$config;
        $this->host=$this->config["host"];
        $this->dbname=$this->config["dbname"];
        $this->user=$this->config["user"];
        $this->pwd=$this->config["pwd"];
        $this->charset=$this->config["charset"];
        //$this->open();
    }

    /*
     * 打开数据库
     */
    public function open()
    {
        $this->conn=new PDO("mysql:host=".$this->host.";dbname=".$this->dbname,$this->user,$this->pwd);
        $this->conn->query('set names '.$this->charset);
    }

    /*
     * 增删改
     */
    public function execSql($sql)
    {
        $bool=$this->conn->exec($sql);
        if($bool>0)
        {
            return "true";
        }else
        {
            return false;
        }
    }

    /*
     * 查询一条数据
    */
    public function quer($sql,$mode=PDO::FETCH_ASSOC)
    {
        $result=$this->conn->query($sql);
        $result->setFetchMode($mode);
        $re=$result->fetch();
        $result=null;
        return $re;
    }

    /*
     * 查询多条数据
    */
    public function querMore($sql,$mode=PDO::FETCH_ASSOC)
    {
        $result=$this->conn->query($sql);
        $result->setFetchMode($mode);
        $re=$result->fetchAll();
        $result=null;
        return $re;
    }

    /*查询指定表中有多少条记录*/
    public function getTabRows($key,$tableName,$where)
    {
        $sql="select count(".$key.") as 'c' from ".$tableName." where ".$where."";
        $result=$this->conn->query($sql);
        $result->setFetchMode(PDO::FETCH_ASSOC);
        $re=$result->fetch();
        $result=null;
        return intval($re['c']);
    }

    /*关闭数据库*/
    public function closeConn()
    {
        $this->conn=null;
    }
}

?>

这个是调用的方法

 <?php
header("content-type:text/html;charset=utf-8");
include "sqlcontrol.class.php";
$config["host"]="localhost";
$config["dbname"]="biaodan";
$config["user"]="root";
$config["pwd"]="root";
$config["charset"]="utf-8";

$db=new dbPdoManger($config);

$db->open();

$sql="INSERT INTO `test` (`name`, `nicheng`, `password`, `sex`, `icon`, `cardid`, `city`, `phone`, `qq`, `mail`, `liuyan`) 
     VALUES ('t', 't', 't', 't, 't', '1315', 'tttt', '598562', '79874564', 'tret', 'werterter')";

echo $db->execSql($sql);

  • 写回答

1条回答 默认 最新

  • xuzuning 2015-03-07 01:10
    关注

    VALUES ('t', 't', 't', 't, 't', '1315', 'tttt', '598562', '79874564', 'tret', 'werterter')";

    显然是漏掉了一个单引号

    另外,PDO 的错误信息要通过 errorInfo 方法获取,或启用异常处理

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!