思路,经过前面面向对象的开发,考虑可以把PHP对MYSQL数据库的操作封装到一个类中,通过面向对象的方式开发。

首先建立 mysql_class.php

 
  
  1. <?  
  2. class mysql {  
  3. private $name;  
  4. private $pass;  
  5. private $host;  
  6. private $table;  
  7. function __construct($host,$name,$pass,$table){  
  8.     $this->host=$host;  
  9.     $this->name=$name;  
  10.     $this->pass=$pass;  
  11.     $this->table=$table;      
  12.     $this->connect();     
  13.     }  
  14. function connect(){  
  15.     $db=mysql_connect($this->host,$this->name,$this->paass) or die (mysql_error());  
  16.     mysql_select_db($this->table,$dbor die ("没有该数据库".$this->table);     
  17.     }  
  18. function query($v)  
  19. {  
  20.     $query = mysql_query($v) ;    
  21.     return $query;    
  22. }  
  23. function fn_select($table,$num){  
  24.     return $this->query("select * from $table limit $num ");  
  25. }  
  26. function fn_insert($table,$name,$value){  
  27.     $this->query("insert into $table ($name)value($value)");  
  28. }  
  29. function error(){  
  30.     return mysql_error();  
  31.     }  
  32. function __call($n,$v){  
  33.     echo "错误的方法为".$n;  
  34.     echo "错误的值为".print_r($v);  
  35.     }      
  36. }  
  37. ?> 

二,在需要的页面用自动加载函数加载实例化对象的方式来输出

 
  
  1. <?  
  2. header("Content-Type:text/html; charset=utf-8");  
  3. function __autoload($name){  
  4.     include ($name."_class.php");  
  5.     }  
  6.     $db = new mysql("localhost","root","","youbaopaidakuo");  
  7.     $db->query("set names utf8");  
  8.     $query = $db->fn_select("ecm_article",20);  
  9.       
  10.     while($arr=mysql_fetch_array($query))  
  11.     {  
  12.         echo $arr[title]."<br>";  
  13.     }  
  14. ?>