config.inc.php內(nèi)容如下
<?php
return array(
'DB_HOST' => '192.168.188.134',
'DB_NAME' => 'scoreboard',
'DB_USER' => 'score',
'DB_PASS' => '123456',
'DB_CHARSET' => 'utf8',
'IS_LOG' => 1,//開(kāi)啟日志
'LOGFILEPATH' => '../log.txt'//日志路徑
);
/*
$database = require('./config.php');
echo $database['DB_TYPE']; //輸出'DB_TYPE'
*/
?>
表設(shè)計(jì)如下
create database scoreboard;
use scoreboard;
create table users(
id int not null auto_increment primary key,
gid int not null, --組id
username varchar(20) not null,
password varchar(32) not null,
sex varchar(2) not null,
totalscore int --個(gè)人總積分
);
create table share(
id int not null auto_increment primary key,
uid int not null,
content varchar(1024) not null, --分享內(nèi)容
comment varchar(1024) not null, --點(diǎn)評(píng)
date varchar(15) not null --分享日期
);
create table score(
id int not null auto_increment primary key,
uid int not null, --用戶id
score int not null, --用戶單次積分
);
grant all privileges on scoreboard.* to 'score'@'%' identified by '123456';
flush privileges;
封裝類如下
<?php
class mysql {
private $logfilepath;
private $is_log;
private $hlog;
private $conn;
//構(gòu)造函數(shù)
public function __construct()
{
$config = include_once(dirname(__FILE__)."/../config/config.inc.php");
$this->is_log = $config['IS_LOG'];
$this->logfilepath = $config['LOGFILEPATH'];
if ($this->is_log){
$handle = fopen($this->logfilepath,"a+");
$this->hlog = $handle;
}
$this->conn = $this->connect($config['DB_HOST'],$config['DB_USER'],$config['DB_PASS'],$config['DB_NAME'],$config['DB_CHARSET']);
}
//連接數(shù)據(jù)庫(kù)
public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbcharset)
{
$this->conn = @mysql_connect($dbhost,$dbuser,$dbpass);
if (!$this->conn) {
$msg = "連接數(shù)據(jù)庫(kù)失斈脸椤:".mysql_error();
$this->write_log($msg);
die($msg);
} else {
if (!@mysql_select_db($dbname)) {
$msg = "連接數(shù)據(jù)庫(kù)成功,但選擇數(shù)據(jù)庫(kù)失斀馑铩:".mysql_error();
$this->write_log($msg);
die($msg);
} else {
$msg = "連接數(shù)據(jù)庫(kù)成功律罢,且選擇數(shù)據(jù)庫(kù)成功";
$this->write_log($msg);
}
}
@mysql_query("set names ".$dbcharset);
}
//執(zhí)行語(yǔ)句
public function query($sql){
$result = @mysql_query($sql);
if (!$result) {
$this->write_log('mysql_query error:'.mysql_error());
} else {
$this->write_log('執(zhí)行語(yǔ)句:'.$sql.' 且執(zhí)行成功');
}
return $result;
}
//查詢一條數(shù)據(jù)
public function select_one($tab,$column = "*",$condition = '',$debug=False) //查詢函數(shù)
{
$condition = $condition ? ' where ' . $condition : NULL;
$sql = "select $column from $tab $condition ";
if ($debug) {
echo '將執(zhí)行語(yǔ)句:'.$sql.'<br />';
} else {
$result = $this->query($sql);
$row = @mysql_fetch_assoc($result);
return $row;
}
}
//查詢多條數(shù)據(jù)
public function select_more($tab,$column = "*",$condition = '',$debug=False) //查詢函數(shù)
{
$condition = $condition ? ' where ' . $condition : NULL;
$sql = "select $column from $tab $condition";
if ($debug) {
echo '將執(zhí)行語(yǔ)句:'.$sql;
} else {
$result = $this->query($sql);
$i = 0;
$rows = array();
while ($row = @mysql_fetch_assoc($result)) {
$rows[$i] = $row;
// print_r($rows[$i]);
$i++;
}
return $rows;
}
}
//返回結(jié)果集
public function echo_result($tab,$column = "*",$condition = '',$debug=False) //查詢函數(shù)
{
$condition = $condition ? ' where ' . $condition : NULL;
$sql = "select $column from $tab $condition ";
if ($debug) {
echo '將執(zhí)行語(yǔ)句:'.$sql.'<br />';
} else {
return $this->query($sql);
}
}
//插入數(shù)據(jù)
public function insert($tab,$arr,$debug=False)
{
$value = '';
$column = '';
foreach ($arr as $k => $v) {
$column .= ",{$k}";
$value .= ",'{$v}'";
}
$column = substr($column, 1);
$value = substr($value, 1);
$sql = "insert into $tab($column) values($value)";
if ($debug) {
echo '將執(zhí)行語(yǔ)句:'.$sql;
} else {
$this->query($sql);
$num = $this->affected_num();
$this->write_log("受影響行數(shù):".$num);
return $num; //返回受影響行數(shù)
}
}
//獲取最后插入的id
public function insert_id() {
$id = mysql_insert_id($this->link_id);
$this->write_log('最后插入的id為:'.$id);
return $id;
}
//更新數(shù)據(jù)
public function update($tab,$arr,$condition = '',$debug=False)
{
if (!$condition) {
die("error".mysql_error());
} else {
$condition = 'where ' . $condition;
}
$value = '';
foreach ($arr as $k => $v) {
$value .= "{$k}='{$v}',";
}
$value = substr($value,0,-1);
$sql = "update $tab set $value $condition";
if ($debug) {
echo '將執(zhí)行語(yǔ)句:'.$sql;
} else {
$this->query($sql);
$num = $this->affected_num();
$this->write_log("受影響行數(shù):".$num);
return $num;
}
}
//刪除數(shù)據(jù)
public function delete($tab,$condition='',$debug=False)
{
$condition = $condition ? ' where ' . $condition : NULL;
$sql = "delete from $tab $condition";
if ($debug) {
echo '將執(zhí)行語(yǔ)句:'.$sql;
} else {
$this->query($sql);
$num = $this->affected_num();
$this->write_log("受影響行數(shù):".$num);
return $num; //返回受影響行數(shù)
}
}
//返回受影響行數(shù)
public function affected_num()
{
$num = @mysql_affected_rows();
return $num;
}
//寫入日志
public function write_log($msg='')
{
if ($this->is_log){
$text = date("Y-m-d H:i:s")." ".$msg."\r\n";
fwrite($this->hlog,$text);
}
}
//關(guān)閉數(shù)據(jù)庫(kù)連接
public function close()
{
mysql_close($this->conn);
}
//析構(gòu)函數(shù)
public function __destruct()
{
if($this->is_log){
fclose($this->hlog);
}
}
}
//$db = new mysql();
// //select_one($tab,$column = "*",$condition = '')
// $rows = $db->select_more('share','*');
// print_r($rows[0]);
// print_r($rows[1]);
// //insert($tab,$arr)
// $arr = array();
// $arr['uid'] = '3';
// $arr['content'] = 'xss';
// $arr['comment'] = 'very good';
// $arr['date'] = '1464082630';
// $db->insert('share',$arr);
// //update($tab,$arr,$condition = '')
// $arr = array();
// $arr['content'] = 'xssxssxssxssxss';
// $arr['comment'] = 'goodgoodgoodgood';
// $condition = 'id > 5';
// $db->update('share',$arr,$condition);
//$db->delete("share","id between 10 and 15");
//$db->close();
?>