<?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){
$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 databases() {
$rsPtr = @mysql_list_dbs($this->conn);
$i = 0;
$cnt = mysql_num_rows($rsPtr);
while ($i < $cnt) {
$rs[] = mysql_db_name($rsPtr, $i);
$i++;
}
return $rs;
}
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(){
return mysql_fetch_array($this->result);
}
public function fetch_assoc() {
return mysql_fetch_assoc($this->result);
}
public function fetch_row() {
return mysql_fetch_row($this->result);
}
public function fetch_Object() {
return mysql_fetch_object($this->result);
}
public function findall($table, $field = '*') {
return $this->query("select $field from $table");
}
public function select($table, $columnName = "*", $condition = '', $debug = '') {
$condition = $condition ? ' Where ' . $condition : NULL;
if ($debug) {
echo "SELECT $columnName FROM $table $condition";
} else {
return $this->query("SELECT $columnName FROM $table $condition");
}
}
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() {
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>";
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理