网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

php调用封装数据库 php如何封装

PHP调用 mysql数据库的问题。

帮助把你代码

创新互联建站专注于企业成都全网营销、网站重做改版、黔江网站定制设计、自适应品牌网站建设、H5技术成都商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为黔江等各大城市提供网站开发制作服务。

分析一下哦

PHP代码:

首先header("Content-Type:text/html;charset=GB2312");

是网页翻转

链接

不过这段感觉有问题!

$dbhost="

localhost

";

本地肯定错

换IP 如202.0.4.45

结果

就正常了$dbuser="root";

这行

账户$dbpw="mysqladmin";

账户

密码

$dbname="chat";

数据库

表名$error_up="br";

输出

换行$dbcharset

=

'gbk'; 字符

通常GBK

就行

这一一分析完

你就知道哪里出错了

早日排错成功!

PHP有办法调用外部数据库配置文件吗

不行的,没有权限

还不如用下面的方法:

直接配置到本地?

1.php里面的的内容可以echo出来,不过要做好安全认证,然后就可以读取了

php调用数据库问题

mysql_connect($host,$root,$pwd);

mysql_select_db($database);

mysql_query("set names utf8");

php封装一个class类实现mysql数据库的增删该查

?php

class db{

private $db;

const MYSQL_OPT_READ_TIMEOUT = 11;

const MYSQL_OPT_WRITE_TIMEOUT = 12;

private $tbl_name;

private $where;

private $sort;

private $fields;

private $limit;

public static $_instance = null;

function __construct(){

$cfg = loadConfig('db');

$db = mysqli_init();

$db-options(self::MYSQL_OPT_READ_TIMEOUT, 3);

$db-options(self::MYSQL_OPT_WRITE_TIMEOUT, 1);

@$db-real_connect($cfg['host'],$cfg['user'],$cfg['pwd'],$cfg['db']);

if ($db-connect_error) {

$this-crash($db-errno,$db-error);

}

$db-set_charset("utf8");

$this-db = $db;

//echo $this-db-stat;

}

public static function getInstance(){

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

self::$_instance = new self();

}

return self::$_instance;

}

private function __clone() {} //覆盖__clone()方法,禁止克隆

public function find($conditions = null){

if($conditions) $this-where($conditions);

return $this-getArray($this-buildSql(),1);

}

public function findAll($conditions = null){

if($conditions) $this-where($conditions);

return $this-getArray($this-buildSql());

}

//表

public function t($table){ $this-tbl_name = $table; return $this;}

//条件

public function where($conditions){

$where = '';

if(is_array($conditions)){

$join = array();

foreach( $conditions as $key = $condition ){

$condition = $this-db-real_escape_string($condition);

$join[] = "`{$key}` = '{$condition}'";

}

$where = "WHERE ".join(" AND ",$join);

}else{

if(null != $conditions) $where = "WHERE ".$conditions;

}

$this-where = $where;

return $this;

}

//排序

public function sort($sort){

if(null != $sort) $sort = "ORDER BY {$sort}";

$this-sort = $sort;

return $this;

}

//字段

public function fields($fields){ $this-fields = $fields; return $this; }

public function limit($limit){$this-limit = $limit; return $this;}

private function buildSql(){

$this-fields = empty($this-fields) ? "*" : $this-fields;

$sql = "SELECT {$this-fields} FROM {$this-tbl_name} {$this-where} {$this-sort}";

accessLog('db_access',$sql);

if(null != $this-limit)$sql .= " limit {$this-limit}";

return $sql;

}

/**

* 返回查询数据

* @param $sql

* @param bool $hasOne

* @return array|bool|mixed

*/

private function getArray($sql,$hasOne = false){

if($this-db-real_query($sql) ){

if ($result = $this-db-use_result()) {

$row = array();

if($hasOne){

$row = $result-fetch_assoc();

}else{

while($d = $result-fetch_assoc()) $row[] = $d;

}

$result-close();

$this-fields = "*";

return $row;

}else{

return false;

}

}else{

if($this-db-error){

$this-crash($this-db-errno,$this-db-error,$sql);

}

}

}

public function findSql($sql,$hasOne = false){

accessLog('db_access',$sql);

if($this-db-real_query($sql) ){

if ($result = $this-db-use_result()) {

$row = array();

if($hasOne){

$row = $result-fetch_assoc();

}else{

while($d = $result-fetch_assoc()) $row[] = $d;

}

$result-close();

$this-fields = "*";

return $row;

}else{

return false;

}

}else{

if($this-db-error){

$this-crash($this-db-errno,$this-db-error,$sql);

}

}

}

public function create($row){

if(!is_array($row))return FALSE;

$row = $this-prepera_format($row);

if(empty($row))return FALSE;

foreach($row as $key = $value){

$cols[] = '`'.$key.'`';

$vals[] = "'".$this-db-real_escape_string($value)."'";

}

$col = implode(',', $cols);

$val = implode(',', $vals);

$sql = "INSERT INTO `{$this-tbl_name}` ({$col}) VALUES ({$val})";

accessLog('db_access',$sql);

if( FALSE != $this-db-query($sql) ){ // 获取当前新增的ID

if($this-db-insert_id){

return $this-db-insert_id;

}

if($this-db-affected_rows){

return true;

}

}

return FALSE;

}

//直接执行sql

public function runSql($sql){

accessLog('db_access',$sql);

if( FALSE != $this-db-query($sql) ){ // 获取当前新增的ID

return true;

}else{

return false;

}

}

public function update($row){

$where = "";

$row = $this-prepera_format($row);

if(empty($row))return FALSE;

foreach($row as $key = $value){

$value = $this-db-real_escape_string($value);

$vals[] = "`{$key}` = '{$value}'";

}

$values = join(", ",$vals);

$sql = "UPDATE {$this-tbl_name} SET {$values} {$this-where}";

accessLog('db_access',$sql);

if( FALSE != $this-db-query($sql) ){ // 获取当前新增的ID

if( $this-db-affected_rows){

return true;

}

}

return false;

}

function delete(){

$sql = "DELETE FROM {$this-tbl_name} {$this-where}";

if( FALSE != $this-db-query($sql) ){ // 获取当前新增的ID

if( $this-db-affected_rows){

return true;

}

}

return FALSE;

}

private function prepera_format($rows){

$columns = $this-getArray("DESCRIBE {$this-tbl_name}");

$newcol = array();

foreach( $columns as $col ){

$newcol[$col['Field']] = $col['Field'];

}

return array_intersect_key($rows,$newcol);

}

//崩溃信息

private function crash($number,$message,$sql=''){

$msg = 'Db Error '.$number.':'.$message ;

if(empty($sql)){

echo t('db_crash');

}else{

$msg .= " SQL:".$sql;

echo t('db_query_err');

}

accessLog('db_error',$msg);

exit;

}

}


新闻标题:php调用封装数据库 php如何封装
URL标题:http://bjjierui.cn/article/ddosjdd.html

其他资讯