扫码关注官方订阅号
84669人学习
65727人学习
82984人学习
467778人学习
498837人学习
471966人学习
256484人学习
152542人学习
224170人学习
139536人学习
81804人学习
85022人学习
11944人学习
20001人学习
60816人学习
5487人学习
15007人学习
2150人学习
6980人学习
194925人学习
359900人学习
1142人学习
19058人学习
3206人学习
180550人学习
48569人学习
17603人学习
40936人学习
1049人学习
750人学习
32909人学习
自己代码一直错误: Fatal error: Call to a member function exec() on null
<?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){
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
精品班
技术支持
技术咨询
学习群
会员优惠
返回顶部
<?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);
}
}