Ryan Castle Ryan Castle - 6 months ago 11
SQL Question

PHP - Creating a table failure

So, I am trying to create a table with PHP on my server, but it is failing to do so even though it is connecting to my server correctly. Here is my code:

// 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";
}


I have tried to find an error in the script, and it looks as if it is located around the
if ($dbSuccess) {
line. It works if I put
if (!$dbSuccess) {
but that would not be helpful as it doesn't fix my problem. Any help will be appreciated.
~Ryan

Answer

You are missing a {

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

Also you will have to close the if statement. Here is the full block corrected

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;
}

}