我自己写三个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);
?>
我发现必须实例化两次 有没有办法不这么麻烦的?小弟是个新手,请问这几段代码有哪些可以优化的
//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.完全没有实例化两次的必要,完全可以实例化一次,多次使用就可以了;
2.可以写一个批量执行SQL的方法,思路,以;号分隔,放到数组中,循环数组执行;
3.也可以看下PHPMYADMIN的源码,或到 www.allniu.com 看看
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);
这样应该完全没有问题啊。。看看表是不是已经存在了。或把你的错误贴出来
没有任何错误提示,源代码也是这样的
你这样写是不会执行第二个sql语句的;sql语句在语法上没错,我单独贴到phpmyadmin上是能建表的
if($this->conn==null) { ===> 你每次都检查conn是否为null,但是第二次进来的时候已经不是null了
$this->connect();
$result = mysql_query($sql); ==> 所以这句就没执行到.....
}