現在掙扎一天。需要在PHP中創建數據庫(如果不存在),並確保它是空的(如果已經存在)。但不知何故,我可能會錯過一些重要的東西,什麼也沒有發生看起來它只是完全跳過創建和刪除部分。PHP不會創建數據庫
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//Database variables
$servername = "localhost";
$username = "Somename";
$password = "Verysecret";
$dbname = "TESTDB";
$temptable = "tablename";
//Open database
$conn = new mysqli($servername, $username, $password,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to database: ",$dbname,"<br>" ;
?><br><?php
//Create table if not existing yet (syntax error here?)
echo "Creating table if non existing","<br>";
$conn->select_db('$dbname');
$sql = "CREATE TABLE IF NON EXIST `{$temptable}` (
`xml_date` datetime,
`xml_duration` int(2),
`xml_boat` VARCHAR(30),
`xml_itinerary` VARCHAR(30),
`xml_dep_arr` VARCHAR(30),
`xml_spaces` INT(2),
`xml_rate_eur` decimal(4,2),
`xml_rate_gbp` decimal(4,2),
`xml_rate_usd` decimal(4,2))";
//This part not showing up in output at all!
if(mysqli_query($conn, $sql)){
echo "Table created successfully";
} else {
echo "Table is not created successfully ";
}
//Deleting rows if table existed already (same syntax error here?)
echo "Making sure table is empty","<br>";
$sql = "DELETE * FROM `{$temptable}`";
mysqli_close($conn);
?>
我看到,當我運行(與MAMP Localy)是:
成功連接到 數據庫:TESTDB
創建表,如果不存在的,確保表是空的
數據庫沒有創建,當我在SequelPro中自己創建它並添加一些行時。
幫助,現在搜索一天!我究竟做錯了什麼?迷失在引號中,回覆引號,雙引號?監督顯而易見的?
'DELETE *'噸帽子的無效加上你還沒有執行它。 –
echo $ sql並在db中運行它,您可能會得到詳細錯誤 – user1844933
CREATE TABLE IF NON EXIST'不正確的語法。請參閱https://dev.mysql.com/doc/refman/5.7/en/create-table.html –