[私はphpの3を勉強しています]Poに自分で書いたデータベースクラスは、後で検索するのに便利です.
3056 ワード
class mysql{
private $conn; // $conn
private $db_host; //
private $db_user;
private $db_password;
private $db_name; //
function __construct($db_host,$db_user,$db_password,$db_name){
// , connect
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_password = $db_password;
$this->db_name = $db_name;
$this->connect();
}
function connect(){
//
$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_password) or die(" ".mysql_errno().":".mysql_error());
mysql_select_db($this->db_name,$this->conn) or die(' ').mysql_error();
mysql_set_charset('utf8');
return $this->conn;
}
// ,$table( ),$array
function insert($table,$array){
$keys = "`".implode("`,`", array_keys($array))."`"; //
$vals = "'".implode("','", array_values($array))."'"; //
$sql = "insert into {$table} ({$keys}) values({$vals})";
$query = mysql_query($sql);
return mysql_insert_id();
}
//
function delete($table,$id,$where=null){
// : delete from table where id = ....
$where = $where ==null?null:'where '.$where;
$sql = "delete from {$table} where id = {$id} limit 1";
$query = mysql_query($sql);
return $query;
}
//
public function select($table,$array,$where=null){
// : select * from table where `user`='$user'......
foreach ($array as $key => $value) {
$select[] = '`'.$key.'`='.$value;
}
$select = implode(' and ', $select);
$where = $where == null?null:$where;
$sql = "select * from {$table} where ".$select.' '.$where;
return $sql;
}
//
function update($table,$array,$where = null){
// : update table set `name`='kopa' where id = ....
foreach ($array as $key => $value) {
$string[] = '`'.$key.'`='.$value;
}
$string =implode('`,',$string);
$where = $where==null?null:" where ".$where;
$sql = "update {$table} set ".$string.$where;
return $sql;
// $query = mysql_query($sql);
// return $query;
// print_r("update {$table} set ".$string.$where);
}
// mysql_num_row
function totalRow($sql){
$query = mysql_query($sql);
$result = mysql_num_rows($query);
return $result;
}
//
function fetch_array($sql){
$query = mysql_query($sql);
$res = mysql_fetch_array($query);
return $res;
}
}
$db = new mysql("localhost",'root','3363064','ctxy');