我自己写三个PHP文件
//db_query.php
header("Content-Type:text/html;charset=utf-8");
class db_query {
    //put your code here
    private $host=' ';
    private $dbuser=' ';
    private $dbpsw=' ';
    private $dbname=' ';
    private $conn=null;
    function  __destruct() {
        $this->close();
    }
    function connect() {
        global $config;
        if($this->conn==null) {
            $this->conn=mysql_connect($this->host, $this->dbuser, $this->dbpsw)
                    or die("Could not connet to databse" . mysql_error());
            mysql_select_db($this->dbname)
                    or die("Could not select databse" . mysql_error());
            mysql_query("set names".$config['charset']);
        }
    }
    function query($sql) {
        if($this->conn==null) {
            $this->connect();
            $result = mysql_query($sql);
        }
        return $result;
    }
    function close() {
        if($this->conn) {
            mysql_close($this->conn);
            $this->conn=null;
        }
    }
}
?>
//config.php
<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
header("Content-Type:text/html;charset=utf-8");
$config=array();
$config['charset']="utf8";
date_default_timezone_set('PRC');
?>//install.php<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
require 'include/db_query.php';
require 'include/config.php';
$db_query=new db_query();
$sql1="create table if not exists bbsmessage (id int auto_increment, username varchar(15) character set utf8,content text character set utf8,PRIMARY KEY (id) )";
$db_query->query($sql1);
$db_query=new db_query();
$sql2="create table if not exists bbsusers (id int auto_increment, username varchar(15) character set utf8,password varchar(15) character set utf8,PRIMARY KEY (id) )";
$db_query->query($sql2);
?>
我发现必须实例化两次 有没有办法不这么麻烦的?小弟是个新手,请问这几段代码有哪些可以优化的

解决方案 »

  1.   

    建议:
    1.完全没有实例化两次的必要,完全可以实例化一次,多次使用就可以了;
    2.可以写一个批量执行SQL的方法,思路,以;号分隔,放到数组中,循环数组执行;
    3.也可以看下PHPMYADMIN的源码,或到 www.allniu.com 看看
      

  2.   

    恩 实例化一次后第二次就用不了了 我试过了 SQL语句没写错 
      

  3.   

    require 'include/db_query.php';
    require 'include/config.php';
    $db_query=new db_query();
    $sql1="create table if not exists bbsmessage (id int auto_increment, username varchar(15) character set utf8,content text character set utf8,PRIMARY KEY (id) )";
    $db_query->query($sql1);
    $sql2="create table if not exists bbsusers (id int auto_increment, username varchar(15) character set utf8,password varchar(15) character set utf8,PRIMARY KEY (id) )";
    $db_query->query($sql2);
    这样应该完全没有问题啊。。看看表是不是已经存在了。或把你的错误贴出来
      

  4.   


    没有任何错误提示,源代码也是这样的
    你这样写是不会执行第二个sql语句的;sql语句在语法上没错,我单独贴到phpmyadmin上是能建表的
      

  5.   

     function query($sql) {
      if($this->conn==null) {    ===> 你每次都检查conn是否为null,但是第二次进来的时候已经不是null了
      $this->connect();
      $result = mysql_query($sql);  ==> 所以这句就没执行到.....
      }
      

  6.   

    private $conn=null;我默认值就是设置的这个啊  有没有什么改进的地方
      

  7.   

    这个默认值只有在初始化一个对象时才执行,你后面给这个赋值后,就再也没有把它置为null了。
      

  8.   

    本帖最后由 xuzuning 于 2010-08-27 10:34:21 编辑