我正在为应用程序创建数据库并且数据库创建正常,但是,我也告诉脚本创建并向数据库添加表,但是它使我在将表添加到表之前刷新页面DB。它偶尔会添加它而不必刷新页面(在初始加载时)。有没有其他人有这个问题?如果是这样,你是如何解决的?这是我的代码:
<?php
$db_name = "815574_pay";
$db_user = "815574_pay99";
$db_pwd = "3Q87v8aL4t76213";
$tbl_name = "accounts";
$connect = mysql_connect("localhost", $db_user, $db_pwd);
$db_create = "CREATE DATABASE " .$db_name;
$use_db = @mysql_select_db($db_name, $connect);
$tbl_create = "CREATE TABLE accounts(id INT(9) NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), fName CHAR(25) NOT NULL, lName CHAR(50) NOT NULL, email VARCHAR(50), password VARCHAR(15) NOT NULL, card INT(16) NOT NULL, ccv INT(4) NOT NULL)";
function db_connect(){
global $db_name;
global $db_user;
global $db_pwd;
global $connect;
if(mysql_connect() == TRUE){
#Make sure the database has connected.
echo "You are connected.<br>
Welcome user $db_user <br>";
}
else{
#Error checking for failed connection.
echo "Failed to connect to MySQL: " .mysql_connect_error();
}
}
db_connect();
function db_create(){
global $db_name;
global $db_user;
global $db_pwd;
global $connect;
global $db_create;
if(mysql_query($db_create, $connect) == TRUE){
#Make sure the database was created
echo "Database $db_name was created.<br>";
}
else{
#Error checking for failed database creation
echo "Error creating database: " .mysql_error() ."<br>";
}
}
db_create();
function tbl_create(){
global $db_name;
global $db_user;
global $db_pwd;
global $connect;
global $tbl_create;
global $use_db;
global $tbl_name;
if($use_db == TRUE){
mysql_query($tbl_create, $connect);
#Make sure the table was successfully added
echo "The table $tbl_name was added to $db_name.";
}
else{
#Error checking for failed table creation
echo "The table could not be added: " .mysql_error();
}
}
tbl_create();
mysql_close($connect);
?>
答案 0 :(得分:0)
您在mysql_select_db
之前致电db_create
。显然,如果数据库不存在,那么就无法选择它。在致电mysql_select_db
并处理db_create
可能遇到的任何错误后,请尝试将来电移至mysql_select_db
。