php操作mysql数据_PHP操作mysql数据库

这篇博客展示了如何使用PHP连接到MySQL数据库并执行基本操作,如创建数据库、创建表、插入数据以及查询数据。代码片段详细地演示了如何使用`mysql_connect()`、`mysql_query()`等函数进行数据库交互。
摘要由CSDN通过智能技术生成

1)连接mysql

$con = mysql_connect("localhost","root","12345");

if(!$con){

die('Could not connect: ' . mysql_error());

}

mysql_close($con);

?>

2)php mysql创建数据库和表

$con=mysql_connect("localhost","root","123456");

if(!$con){

die('Could not connect: ' . mysql_error()');

}

if(mysql_query("create database my_db",$con));

{

echo "database created";

}else

{

echo "Error create database: '". mysql_error();

}

mysql_close($con);

?>

3)php mysql创建数据库和表

$con = mysql_connect("localhost","root","123456");

if(!$con){

die('Could not connect: ' . mysql_error()');

}

if(mysql_query("crate database my_db",$con)){

echo "database careated";

}else{

echo "Error create database:" .mysql_error();

}

mysql_select_db("my_db",$con);

$sql = "CREATE TABLE Persons

(

FirstName varchar(15),

LastName varchar(15),

Age int

)";

mysql_query($sql,$con);

mysql_close($con);

?>

3)php mysql创建数据库和表

$con=mysql_connect("localhost","root","123456");

if($con){

die ('Could not connect:' .mysql_error());

}

if(mysql_query("create database my_db",$con));

{

echo "database created";

}else

{

echo "Error create database: '". mysql_error();

}

mysql_select_db("my_db",$con);

mysql_query("INSERT INTO Persons (FirstName, LastName, Age)

VALUES ('Peter', 'Griffin', '35')");

mysql_query("NSERT INTO Persons (FirstName, LastName, Age)

VALUES ('Glenn', 'Quagmire', '33')");

mysql_close($con);

?>

4)php mysql创建表

$con = mysql_connect("loaclhost","root","123456");

if(!$con){

die('could not conenct:' .mysql_error());

}

//创建数据库

if(mysql_query("create database my_db",$con));

{

echo "database created";

}else

{

echo "Error create database: '". mysql_error();

}

//选择创建的数据库my_db

mysql_select_db("my_db",$con);

//创建表

mysql_select_db("my_db",$con);

$sql = "CREATE TABLE Persons

(

FirstName varchar(15),

LastName varchar(15),

Age int

)";

mysql_query($sql,$con);

//插入数据

$sql_in ="INSERT INTO Persons (FirstName, LastName, Age)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if(!mysql_query($sql_n,$con)){

die('Error:' .mysql_error());

}

echo "1 row added";

mysql_close($con);

?>

5)php 查询mysql数据

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "

FirstnameLastname

while($row = mysql_fetch_array($result))

{

echo "

";

echo "

" . $row['FirstName'] . "";

echo "

" . $row['LastName'] . "";

echo "

";

}

echo "

";

mysql_close($con);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值