一些东西.
有一个缺少的分号;在…和结尾)“
if(empty($result)) {
echo "
" . $table . " table does not exist
";$query = "CREATE TABLE IF NOT EXISTS WEIGHIN_DATA (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
DATE DATE NOT NULL,
VALUE SMALLINT(4) UNSIGNED NOT NULL
)" //
这会导致/抛出一个解析错误,例如:
Parse error: syntax error, unexpected ‘}’ in…
在您最初发布的代码的评论中显示的其他错误中.
另外,您在创建表时没有使用mysql_query.
这是一个mysqli_方法,我在其中注释掉了您的原始代码.
旁注:您在$query =“SELECT ID FROM”中为您的列使用ID. $表;然而你用小写的id创建你的表和列;两个字母都必须匹配.
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$DB_HOST = "xxx"; // put your own data
$DB_NAME = "xxx"; // put your own data
$DB_USER = "xxx"; // put your own data
$DB_PASS = "xxx"; // put your own data
$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}
/*
// 1. CONNECT TO THE DB SERVER, confirm connection
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "
Connected to MySQL
";$mysql_connexn = mysql_connect("localhost", "root", ""); // redundant ?
// 2. CONNECT TO THE SPECIFIED DB, confirm connection
$db = "weighttracker";
mysql_select_db($db) or die(mysql_error());
echo "
Connected to Database '$db'
";$db_connexn = mysql_select_db($db)or die(mysql_error("can\'t connect to $db"));
// 3. if table doesn't exist, create it
$table = "WEIGHIN_DATA";
$query = "SELECT ID FROM " . $table; // that should be id and not ID
//$result = mysql_query($mysql_connexn, $query);
$result = mysql_query($query, $mysql_connexn);
*/
$table = "WEIGHIN_DATA";
$query = "SELECT ID FROM " . $table; // that should be id and not ID
//$result = mysql_query($mysql_connexn, $query); // your original code
// however connection comes last in mysql method, unlike mysqli
$result = mysqli_query($conn,$query);
if(empty($result)) {
echo "
" . $table . " table does not exist
";$query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS WEIGHIN_DATA (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
DATE DATE NOT NULL,
VALUE SMALLINT(4) UNSIGNED NOT NULL
)");
}
else {
echo "
" . $table . "table exists
";} // else
?>