mysql创建表如果不存在,创建mysql表(如果不存在)

I don't work much with php/mysql, but I need what I thought would be a relatively straightforward task: to check if a table exists and create it if it doesn't. I can't even get a useful error message and there's no table being created in the db. There is obviously something wrong with my syntax.

session_start();

error_reporting(E_ALL);

ini_set('display_errors', 1);

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

//$result = mysql_query($mysql_connexn, $query);

$result = mysql_query($query, $mysql_connexn);

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

)"

}

else {

echo "

" . $table . "table exists

";

} // else

?>

解决方案

A few things.

There was a missing semi-colon ; in and at the end of )"

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

)" //

which would have caused/thrown a parse error, such as:

Parse error: syntax error, unexpected '}' in...

Amongst other errors as shown in my comments from your originally posted code.

Plus, you were not using mysql_query in your table creation.

Here is a mysqli_ method, where I commented out your original codes.

Sidenote: You're using ID for your column in $query = "SELECT ID FROM " . $table; and yet you create your table and column as id in lowercase; both lettercase must match.

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

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值