这是一款比较全的mysql操作类,昨天写了一个简单的连接mysql数据库代码,相对于这个来说,那个是最简单的了,这个是一款包括数据查询,更新,删除,等操作,实例代码如下:
- 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 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 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() {
- 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>";
- }
- }
-
- $mysql = new mysql($dbhost, $dbuser, $dbpwd, $dbname, $coding);
|