Php的大神,请帮我看一下这个类引入了config文件,但是这个文件的配置应该怎么写呀?代码如下 50
classDB{//定义私有属性private$host;private$port;private$username;private$password;private$d...
class DB{
//定义私有属性
private $host;
private $port;
private $username;
private $password;
private $dbname;
private $charset;
private $dbtype;
private $pdo;
//定义构造函数自动加载配置文件
function __construct(){
//加载配置文件
include_once('./config/config.php');
//给属性赋值
$this->dbtype = $config['db'];
$this->host = $config['host'];
$this->username = $config['username'];
$this->password = $config['password'];
$this->charset = $config['charset'];
$this->port = $config['port'];
$this->dbname = $config['dbname'];
//pdo连接数据库
$this->pdo = new PDO("$this->dbtype:host=$this->host;dbname=$this->dbname","$this->username","$this->password");
//发送编码
$this->pdo->query("set names $this->charset");
}
/*
* 定义执行查询sql语句的方法
* 参数: 查询sql语句
* 返回: 二维关联数组
*/
public function query($sql){
$res = $this->pdo->query($sql);
$res->setFetchMode(PDO::FETCH_ASSOC);
$arr = $res->fetchAll();
return $arr;
}
/**
* 查询一行记录的方法
* 参数:表名 条件(不包含where)
* 返回:一维关联数组
*/
public function getRow($tablename,$where){
//组装sql语句
$sql = "select * from $tablename where $where";
//查询
$res = $this->pdo->query($sql);
$res->setFetchMode(PDO::FETCH_ASSOC);
$arr = $res->fetch();
return $arr;
}
/**
* 查询全部记录
* 参数:表名
* 返回:二维关联数组
*/
public function getAll($tablename){
$res = $this->pdo->query("select * from $tablename");
$res->setFetchMode(PDO::FETCH_ASSOC);
$arr = $res->fetchAll();
return $arr;
}
/**
* 查询某个字段
* 参数: 字段名(多个的话用逗号隔开) 表名 条件(不含where)
* 返回: 二维关联数组
*/
public function getOne($column,$tablename,$where="1"){
//拼接sql语句
$sql = "select $column from $tablename where $where";
$rs = $this->pdo->query($sql);
$rs->setFetchMode(PDO::FETCH_ASSOC);
//$col = $rs->fetchColumn();
$col = $rs->fetchAll();
return $col;
}
/**
* 查询最后一次插入的数据
* 参数:表名
* 返回:数组
*/
public function getlastone($tablename){
$sql = "select * from $tablename where id=(select max(id) from $tablename)";
$res = $this->pdo->query($sql);
$res->setFetchMode(PDO::FETCH_ASSOC);
$arr = $res->fetch();
return $arr;
} 展开
//定义私有属性
private $host;
private $port;
private $username;
private $password;
private $dbname;
private $charset;
private $dbtype;
private $pdo;
//定义构造函数自动加载配置文件
function __construct(){
//加载配置文件
include_once('./config/config.php');
//给属性赋值
$this->dbtype = $config['db'];
$this->host = $config['host'];
$this->username = $config['username'];
$this->password = $config['password'];
$this->charset = $config['charset'];
$this->port = $config['port'];
$this->dbname = $config['dbname'];
//pdo连接数据库
$this->pdo = new PDO("$this->dbtype:host=$this->host;dbname=$this->dbname","$this->username","$this->password");
//发送编码
$this->pdo->query("set names $this->charset");
}
/*
* 定义执行查询sql语句的方法
* 参数: 查询sql语句
* 返回: 二维关联数组
*/
public function query($sql){
$res = $this->pdo->query($sql);
$res->setFetchMode(PDO::FETCH_ASSOC);
$arr = $res->fetchAll();
return $arr;
}
/**
* 查询一行记录的方法
* 参数:表名 条件(不包含where)
* 返回:一维关联数组
*/
public function getRow($tablename,$where){
//组装sql语句
$sql = "select * from $tablename where $where";
//查询
$res = $this->pdo->query($sql);
$res->setFetchMode(PDO::FETCH_ASSOC);
$arr = $res->fetch();
return $arr;
}
/**
* 查询全部记录
* 参数:表名
* 返回:二维关联数组
*/
public function getAll($tablename){
$res = $this->pdo->query("select * from $tablename");
$res->setFetchMode(PDO::FETCH_ASSOC);
$arr = $res->fetchAll();
return $arr;
}
/**
* 查询某个字段
* 参数: 字段名(多个的话用逗号隔开) 表名 条件(不含where)
* 返回: 二维关联数组
*/
public function getOne($column,$tablename,$where="1"){
//拼接sql语句
$sql = "select $column from $tablename where $where";
$rs = $this->pdo->query($sql);
$rs->setFetchMode(PDO::FETCH_ASSOC);
//$col = $rs->fetchColumn();
$col = $rs->fetchAll();
return $col;
}
/**
* 查询最后一次插入的数据
* 参数:表名
* 返回:数组
*/
public function getlastone($tablename){
$sql = "select * from $tablename where id=(select max(id) from $tablename)";
$res = $this->pdo->query($sql);
$res->setFetchMode(PDO::FETCH_ASSOC);
$arr = $res->fetch();
return $arr;
} 展开
1个回答
展开全部
首先在 config.php 中直接返回一个数组;例如:
<?php
return [
'db' => 'db',
'host' => 'host',
'username' => 'username',
... ...
];
然后在 DB 类中引入的时候这么写:
$config = include_once( 'config.php' ); // 这样 $config 变量就是 config.php 文件中返回的数组,后面就可以直接从 $config 这个数组变量中取值了;
// $config['db']
// $config['host']
... ...
<?php
return [
'db' => 'db',
'host' => 'host',
'username' => 'username',
... ...
];
然后在 DB 类中引入的时候这么写:
$config = include_once( 'config.php' ); // 这样 $config 变量就是 config.php 文件中返回的数组,后面就可以直接从 $config 这个数组变量中取值了;
// $config['db']
// $config['host']
... ...
更多追问追答
追问
为什么我写入数据库的中文字都是?号?我已经设置编码过了,还是乱码
就是这段程序
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询