php mysql 连接 类_PHP数据库连接类

小编典典

首先构建一个MySQL类库…满足以下示例中的要求:

include '../config/Dbconfig.php';

class Mysql extends Dbconfig {

public $connectionString;

public $dataSet;

private $sqlQuery;

protected $databaseName;

protected $hostName;

protected $userName;

protected $passCode;

function Mysql() {

$this -> connectionString = NULL;

$this -> sqlQuery = NULL;

$this -> dataSet = NULL;

$dbPara = new Dbconfig();

$this -> databaseName = $dbPara -> dbName;

$this -> hostName = $dbPara -> serverName;

$this -> userName = $dbPara -> userName;

$this -> passCode = $dbPara ->passCode;

$dbPara = NULL;

}

function dbConnect() {

$this -> connectionString = mysql_connect($this -> serverName,$this -> userName,$this -> passCode);

mysql_select_db($this -> databaseName,$this -> connectionString);

return $this -> connectionString;

}

function dbDisconnect() {

$this -> connectionString = NULL;

$this -> sqlQuery = NULL;

$this -> dataSet = NULL;

$this -> databaseName = NULL;

$this -> hostName = NULL;

$this -> userName = NULL;

$this -> passCode = NULL;

}

function selectAll($tableName) {

$this -> sqlQuery = 'SELECT * FROM '.$this -> databaseName.'.'.$tableName;

$this -> dataSet = mysql_query($this -> sqlQuery,$this -> connectionString);

return $this -> dataSet;

}

function selectWhere($tableName,$rowName,$operator,$value,$valueType) {

$this -> sqlQuery = 'SELECT * FROM '.$tableName.' WHERE '.$rowName.' '.$operator.' ';

if($valueType == 'int') {

$this -> sqlQuery .= $value;

}

else if($valueType == 'char') {

$this -> sqlQuery .= "'".$value."'";

}

$this -> dataSet = mysql_query($this -> sqlQuery,$this -> connectionString);

$this -> sqlQuery = NULL;

return $this -> dataSet;

#return $this -> sqlQuery;

}

function insertInto($tableName,$values) {

$i = NULL;

$this -> sqlQuery = 'INSERT INTO '.$tableName.' VALUES (';

$i = 0;

while($values[$i]["val"] != NULL && $values[$i]["type"] != NULL) {

if($values[$i]["type"] == "char") {

$this -> sqlQuery .= "'";

$this -> sqlQuery .= $values[$i]["val"];

$this -> sqlQuery .= "'";

}

else if($values[$i]["type"] == 'int') {

$this -> sqlQuery .= $values[$i]["val"];

}

$i++;

if($values[$i]["val"] != NULL) {

$this -> sqlQuery .= ',';

}

}

$this -> sqlQuery .= ')';

#echo $this -> sqlQuery;

mysql_query($this -> sqlQuery,$this ->connectionString);

return $this -> sqlQuery;

#$this -> sqlQuery = NULL;

}

function selectFreeRun($query) {

$this -> dataSet = mysql_query($query,$this -> connectionString);

return $this -> dataSet;

}

function freeRun($query) {

return mysql_query($query,$this -> connectionString);

}

}

?>

和配置文件…

class Dbconfig {

protected $serverName;

protected $userName;

protected $passCode;

protected $dbName;

function Dbconfig() {

$this -> serverName = 'localhost';

$this -> userName = 'root';

$this -> passCode = 'pass';

$this -> dbName = 'dbase';

}

}

?>

2020-05-17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值