登录  /  注册
谁有源代码,贴一下
木俞
木俞 2018-03-14 10:48:36
[PHP讨论组]

自己代码一直错误: Fatal error: Call to a member function exec() on null 

木俞
木俞

全部回复(1)
秋凉

<?php


class db{

private $dbConfig  = [

'db'  => 'mysql',

'host'  => 'localhost',

'port'  => '3306',

'dbname'   =>  'one',

'user'  => 'root',

'password'  => '1234',//

'charset'  => 'utf'

];


public $insertId  = null;


public $num = 0;

private static $instance  =  null;


private $conn  = null;



private function __construct($params){

$this->dbConfig  = array_merge($this->dbConfig,$params);

$this->conncet();

}

private function __clone(){


}

public function getInstance(){

if(!self::$instance instanceof self){

self::$instance   = new self();

}

return self::$instance;

}

public function connect(){

try{

$dsn  = "{$this->dbConfig['db']}:{$this->dbConfig['host']};

port  = {$this->dbConfig['port']};

charset  = {$this->dbConfig['charset']}";

$this->conn  = new pdo($dsn,$this->dbConfig['user'],$this->dbConfig['password']);

}

catch(PDOExecption $e){

die("数据库连接失败".$e->getMessage());

}

}

// 完成数据表的写操作 新增 更新 删除

// 返回受影响的记录 如果新增还返回新增主键id

public function exec($sql){

$num = $this->conn->exec($sql);

// 如果有受影响的记录

if($num>0){

// 如果是新增操作 初始化新增主键ID的属性

if(null !==$this->conn->lastInsertId()){

$this->insertId = $this->conn->lastInsertId();

}

$this->num = $rum;//返回受影响的记录

}else{

$error = $this->conn->errorInfo(); //获取最后操作的错误信息 【0】错误标识符 【1】错误代码【2】错误信息

print '操作失败'.$error[0].':'.$error[1].':'.$error[2];

}

}


//获取单条查询信息

public function fetch ($sql )

{

return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC);

}

public function fetchALL($sql){

return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC);

}

}


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号