PHP - 创建表失败

时间:2014-04-09 12:20:52

标签: php mysql sql web html-table

所以,我正在尝试在我的服务器上用PHP创建一个表,但即使它正确地连接到我的服务器也没有这样做。这是我的代码:

// alphacrmTableCreate.php | Ryan Castle | 09/04/2014 - This script CREATEs sables     tCompany and tPerson in the database 'alphacrm'

{ // Connect and test mySQL and specific DB

$hostname = "localhost";
$username = "root";
$password = "";

$databaseName = "alphacrm";

$dbConnected = @mysql_connect($hostname, $username, $password);
$dbSelected = @mysql_select_db($databaseName, $dbConnected);

$dbSuccess = true;
if ($dbConnected) {
    if (!$dbSelected) {
        echo "DB connection FAILED<br><br>";
        $dbSuccess = false;
    } else {
        echo "DB connection SUCCESSFUL<br><br>";
        $dbSuccess = true;
    }
} else
    echo "mySQL connection FAILED<br><br>";
    $dbSuccess = false;
}

if ($dbSuccess) {
        //Table script here
    echo "Table created";
} else {
    echo "Table creation has failed";
}

我试图在脚本中找到错误,看起来好像它位于if ($dbSuccess) {行周围。如果我放if (!$dbSuccess) {,它会起作用,但这不会有帮助,因为它不能解决我的问题。任何帮助将不胜感激。 〜瑞恩

1 个答案:

答案 0 :(得分:6)

你错过了{

 else
    echo "mySQL connection FAILED<br><br>";
    $dbSuccess = false;
}

此外,您还必须关闭if语句。这是完整的块校正

if ($dbConnected) {
    if (!$dbSelected) {
        echo "DB connection FAILED<br><br>";
        $dbSuccess = false;
    } else {
        echo "DB connection SUCCESSFUL<br><br>";
        $dbSuccess = true;
    }
} else {
    echo "mySQL connection FAILED<br><br>";
    $dbSuccess = false;
}

}