0

我正在为应用程序创建一个数据库并且数据库创建得很好,但是,我还告诉脚本创建一个表并将其添加到数据库中,但它让我在将表添加到数据库之前刷新页面。它偶尔会添加它,而无需我刷新页面(在初始加载时)。有没有其他人有这个问题?如果是这样,你是如何解决的?这是我的代码:

<?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);
?>
4

1 回答 1

0

mysql_select_db之前打过电话db_create。显然,如果数据库不存在,那么你就不能选择它。尝试在调用mysql_select_db之后将调用移至db_create并处理可能出现的任何错误mysql_select_db

于 2013-06-16T23:13:03.453 回答