|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
可以说你的马步已经扎的差不多了,接下来就要开始练把势的时候了,如果有条件的话,用笔或者打印一个简易的PHP手册在身上,时不时的摸出来看看,记得,去WC也不能放过(^2^)。 Mysql.class.php 下载
复制代码 代码以下:
<?php
class Mysql {
private $db_host; //主机地址
private $db_user; //用户名
private $db_pass; //毗连暗码
private $db_name; //称号
private $db_charset; //编码
private $conn;
public $debug=false;//调试开关,默许封闭
private $query_id; //用于判别sql语句是不是履行胜利
private $result; //了局集
private $num_rows; //了局集中行的数量,仅对select无效
private $insert_id; //上一步 INSERT 操作发生的 ID
// 机关/析构函数
function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) {
$this->db_host = $db_host ;
$this->db_user = $db_user ;
$this->db_pass = $db_pass ;
$this->db_name = $db_name ;
$this->db_charset = $db_charset ;
$this->conn = $conn ;
$this->connect();
}
function __destruct () {
@mysql_close($this->conn);
}
// 毗连/选择数据库
public function connect () {
if ($this->conn == 'pconn') {
@$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass);
} else {
@$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
}
if (!$this->conn) {
$this->show_error('数据库-毗连掉败:用户名或暗码毛病!');
}
if (!@mysql_select_db($this->db_name,$this->conn)) {
$this->show_error("数据库-选择掉败:数据库 $this->db_name 不成用");
}
mysql_query("SET NAMES $this->db_charset");
return $this->conn;
}
// query办法
public function query ($sql) {
if ($this->query_id) $this->free_result();
$this->query_id = @mysql_query($sql,$this->conn);
if (!$this->query_id) $this->show_error("SQL语句 <b>\"$sql\"</b> 履行时碰到毛病");
return $this->query_id;
}
// 显示具体毛病信息
public function show_error ($msg) {
if($this->debug){
$errinfo = mysql_error();
echo "毛病:$msg <br/> 前往:$errinfo<p>";
}else{
echo '<p>呈现毛病!<p>';
}
}
// 取得query履行胜利与否的信息
public function get_query_info($info){
if ($this->query_id) {
echo $info;
}
}
// 查询一切
public function findall ($table_name) {
$this->query("select * from $table_name");
}
// mysql_fetch_array
public function fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array($this->query_id);
return $this->result;
}
}
// ......
public function fetch_assoc () {
if ($this->query_id) {
$this->result = mysql_fetch_assoc($this->query_id);
return $this->result;
}
}
public function fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row($this->query_id);
return $this->result;
}
}
public function fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object($this->query_id);
return $this->result;
}
}
// 获得 num_rows
public function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows($this->query_id);
return $this->num_rows;
}
}
// 获得 insert_id
public function insert_id () {
return $this->insert_id = mysql_insert_id();
}
// 显示共有几何张表
public function show_tables () {
$this->query("show tables");
if ($this->query_id) {
echo "数据库 $this->db_name 共有 ".$this->num_rows($this->query_id)." 张表<br/>";
$i = 1;
while ($row = $this->fetch_array($this->query_id)){
echo "$i -- $row[0]<br/>";
$i ++;
}
}
}
// 显示共有几何个数据库
public function show_dbs(){
$this->query("show databases");
if ($this->query_id) {
echo "共无数据库 ".$this->num_rows($this->query_id)." 个<br/>";
$i = 1;
while ($this->row = $this->fetch_array($this->query_id)){
echo "$i -- ".$this->row[Database]."<br />";
$i ++;
}
}
}
// 删除数据库:前往删除了局
public function drop_db ($db_name='') {
if ($db_name == '') {
$db_name = $this->db_name;//默许删除以后数据库
$this->query("DROP DATABASE $db_name");
}else {
$this->query("DROP DATABASE $db_name");
}
if ($this->query_id) {
return "数据库 $db_name 删除胜利";
}else {
$this->show_error("数据库 $db_name 删除掉败");
}
}
// 删除数据表:前往删除了局
public function drop_table ($table_name) {
$this->query("DROP TABLE $table_name");
if ($this->query_id) {
return "数据表 $table_name 删除胜利";
}else {
$this->show_error("数据表 $table_name 删除掉败");
}
}
// 创立数据库
public function create_db ($db_name) {
$this->query("CREATE DATABASE $db_name");
if($this->query_id){
return "数据库 $db_name 创立胜利";
}else {
$this->show_error("数据库 $db_name 创立掉败");
}
}
// 获得数据库版本
public function get_info(){
echo mysql_get_server_info();
}
// 释放内存
public function free_result () {
if ( @mysql_free_result($this->query_id) )
unset ($this->result);
$this->query_id = 0;
}
} // End class
?>
当然你可以把你最基本的功能放出来的时候就放出来,比如放到论坛上,让大家都参与, |
|