<?php
class mysql{
private $db_host;
private $db_user;
private $db_pwd;
private $db_database;
private $conn;
private $sql;
private $result;
private $coding;
private $show_error = true;
public function __construct($db_host, $db_user, $db_pwd, $db_database, $coding = 'utf8'){
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_database = $db_database;
$this->coding = $coding;
$this->connect();
}
private function connect(){
$this->conn = @mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
if(!$this->conn){
if($this->show_error){
$this->show_error('错误提示:链接数据库失败!');
}
}
if(!@mysql_select_db($this->db_database, $this->conn)){
if($this->show_error){
$this->show_error('错误提示:打开数据库失败!');
}
}
if(!@mysql_query("SET NAMES $this->coding")){
if($this->show_error){
$this->show_error('错误提示:设置编码失败!');
}
}
}
public function query($sql){
$this->sql = $sql;
$result = mysql_query($this->sql, $this->conn);
if(!$result){
$this->show_error('错误的SQL语句:', $this->sql);
}else{
return $this->result = $result;
}
}
public function show_databases(){
$this->query("show databases");
echo '现有数据库:' . mysql_num_rows($this->result);
echo "<br />";
$i = 1;
while($row=mysql_fetch_array($this->result)){
echo "$i $row[Database]" . "<br />";
$i++;
}
}
public function show_tables(){
$this->query("show tables");
echo "数据库{$this->db_database}共有". mysql_num_rows($this->result) . "张表:";
echo "<br />";
$column_name = "Tables_in_" . $this->db_database;
$i = 1;
while($row=mysql_fetch_array($this->result)){
echo "$i $row[$column_name]" . "<br />";
$i++;
}
}
public function fetch_array($result=''){
if($this->result){
return mysql_fetch_array($this->result);
}else{
return mysql_fetch_array($result);
}
}
public function findall($table, $field = '*') {
return $this->query("SELECT $field FROM $table");
}
public function delete($table, $condition) {
return $this->query("DELETE FROM $table WHERE $condition");
}
public function insert($table, $field, $value) {
return $this->query("INSERT INTO $table ($field) VALUES ('$value')");
}
public function update($table, $update_content, $condition) {
return $this->query("UPDATE $table SET $update_content WHERE $condition");
}
public function insert_id() {
return mysql_insert_id();
}
public function num_rows() {
if ($this->result == null) {
if ($this->show_error) {
$this->show_error('SQL语句错误', '请检查是否已经使用了query()方法,并成功查询且返回了资源标识符?');
}
} else {
return mysql_num_rows($this->result);
}
}
public function num_fields($table) {
$this->query("select * from $table");
echo "<br />";
echo '字段数:' . $total = mysql_num_fields($this->result);
echo "<pre>";
for ($i = 0; $i < $total; $i++) {
print_r(mysql_fetch_field($this->result, $i));
}
echo "</pre>";
echo "<br />";
}
public function show_error($message='',$sql=''){
echo "<fieldset>";
echo "<legend>错误信息提示:</legend><br />";
echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>";
echo '错误原因:'. mysql_error() . "<br /><br />";
echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>";
echo "<font color='white'>" . $message . "</font>";
echo "</div>";
echo "<font color='red'><pre>" . $sql . "</pre></font>";
echo "</div>";
echo "</fieldset>";
}
}
?>
分享到:
相关推荐
PHP MYSQL操作类 PHP_DataSet
PHP MYSQL 操作类
php中mysql数据库操作类
一款比较全的mysql操作类,这个是一款包括数据查询,更新,删除,等操作,有需要的小伙伴可以下载学习和参考,也可以自己进行修改操作,达到自己的目的。
$conn = DB::instance(array('dbname'=>'default')); or $conndb = DB::instance(array( 'host' => 'localhost', 'dbname' => 'default', 'username' => 'root', 'password' => '', 'charset' => 'utf8' ...
多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加 多个关键字请用空格分隔,最多填写5个。点击右侧Tag快速添加
比较方便的一个类,分享了 比较适合初中级开发人员,比较实用
新手学习,php7中mysql数据库操作类实例,非常清晰易懂
php中mysql数据库操作类
php实现MySQL数据库操作类
自己根据 @金山 曾经写的一个php mysql 操作类优化的mysql类库
php操作mysql数据库操作类,包括数据库连接及操作
PHP基于pdo的数据库操作类【可支持mysql、sqlserver及oracle】.docx
自己用php5写的mysql数据库操作类,功能很齐全,前段时间上传了一次,但是发现了一点错误,现在修正过来了,还加了一个管理员判断的功能,自己看吧,反正很好用,我一直在用的。传上来赚点分数,保证你的分花的不亏...
介绍一个不错的PHP mysql操作类,SQL执行后的数据保存变量,读取一条数据并把数据往后移一位,如果数据为空则返回为null,更新数据库里面的数据,$t为表名,$v格式为数组格式,上标为字段名,下标为数据;$w为条件上标...
一个完整的php操作MySQL数据库类,需要的朋友可以下载。
分享一个完整的php操作MySQL数据库类,需要的朋友可以下载。如果朋友们有更好的此类库可以上传到php中文网与大家一起学习分享。
一个php开发的万能MySQL数据库连接类,完全开放的源代码,可以自由修改
今天刚做的一个MySQL操作类,基本功能已经完成,支持连贯操作,支持基本的增改删差,查询条件和数据插入支持数组方式。自动缓存管理,可以选择文件缓存,或者memcache(也可以很方便的扩展其他缓存方式)。 使用...
一个php使用单例模式实现mysql类,封装mysql操作类,包括连接功能,及查询功能,封装getAll,getRow,getOne,afftect_rows()方法。