$con=mysql_connect("127.0.0.1:3306","ken","123456");
if(!$con)
{
die('Could not connect: '.mysql_error()."
");
}
//创建数据库
if(mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database Created"."
";
}
else
{
echo "Error Creating database:".mysql_error()."
";
}
//选择数据库
mysql_select_db("my_db",$con);
//创建表
$sql_table = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_query($sql_table,$con);
//获取数据
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$age=$_POST["age"];
//sql语句
$sql_insert="INSERT INTO Persons VALUES ( '$firstname', '$lastname' , '$age')";
if(!mysql_query($sql_insert,$con))
{
die("Error:".mysql_error());
}
echo "1 recond added"."
";
//查询
$result = mysql_query("SELECT * FROM Persons");
echo "
Firstname | Lastname | Age |
---|
while($row = mysql_fetch_array($result))
{
echo "
";echo "
" . $row['FirstName'] . "";echo "
" . $row['LastName'] . "";echo "
" . $row['Age'] . "";echo "
";}
mysql_close($con);
?>