我收到“表创建成功”消息但没有创建表。
<html>
<body>
<?php
$con=mysql_connect("localhost","root","");
//create db
mysql_query("CREATE DATABASE VashDedomenwn2", $con);
echo "Db creation success <br>";
//create table
$sql= mysql_query("CREATE TABLE VashDedomenwn2.phone_book
(
personID int NOT NULL,
PRIMARY KEY(person ID),
LastName varchar(20) NOT NULL,
FirstName varchar(20),
Address varchar(30),
Age int,
Phone varchar(10)
)
");
mysql_query($sql,$con);
echo "Table creation success <br>";
//END CONNECTION
mysql_close($con);
?>
</body>
</html>
我是php的新手!可能这是一个愚蠢的错误...感谢帮助
答案 0 :(得分:0)
在创建表之前,必须打开您创建的数据库。
//create db
mysql_query("CREATE DATABASE VashDedomenwn2", $con);
echo "Db creation success <br>";
mysql_select_db("database name", $con);
//create table
...
并改变
$sql= mysql_query("CREATE TABLE VashDedomenwn2.phone_book
(
...
)
");
到
$sql= mysql_query("CREATE TABLE `VashDedomenwn2.phone_book`
(
...
)
");
好的,正如@php_purest所说,使用mysqli更好。 Mysqli是支持OOPS的改进版mysql。它可以降低服务器的压力。
像这样:
$conn = new mysqli("localhost","root","");
$conn->query("CREATE DATABASE VashDedomenwn2");
echo "Db creation success <br>";
$conn->select_db("VashDedomenwn2");
//create table
$conn->query("CREATE TABLE `VashDedomenwn2.phone_book`
(
personID int NOT NULL PRIMARY KEY,
LastName varchar(20) NOT NULL,
FirstName varchar(20),
Address varchar(30),
Age int,
Phone varchar(10)
)
");
echo "Table creation success <br>";
//END CONNECTION
$conn->close();