
php实现增删查改步骤如下:
1、建立到数据库的连接
php本身提供腿mysql数据库的支持,使用mysql_connect函数来连接,语法如下:
resource mysql_connect([string server [, string username [,string password [, bool new_link [, int_client_flags]]]]])
该函数用来打开或重复使用一个到mysql服务器的连接。server是需要连接的mysql服务器,可以包括端口号使用英文的冒号隔开,例如“hostname:port”。username和password分别是连接数据时所需的用户名和密码。new_link参数默认为FALSE,如果用同样的参数第二次调用mysql_connect(),将不会建立新连接,而将返回已经打开的连接标识。设置为false时,则总是打开新的连接。
<p>连接到mysql数据库</p>
<?php header("Content-Type: text/html;charset=utf-8");
error_reporting(0); //禁止错误输出
$link = mysqli_connect('127.0.0.1:3306','root','root'); //创建数据库连接
if(!$link){ //如果失败
die('连接mysql数据库失败'.mysqli_error()); //显示出错误信息
}
echo '连接mysql服务器成功!'; //否则显示连接成功的信息
mysqli_close($link); //最后关闭数据库连接
2、创建数据库和数据表
<p>创建mydbdemo数据库</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<?php header("Content-Type: text/html;charset=utf-8");
error_reporting(0); //禁止错误输出
$con = mysqli_connect('127.0.0.1:3306','root','root'); //创建数据库连接
if(!$con){ //如果失败
die('连接mysql数据库失败'.mysqli_error()); //显示出错误信息
}
if(mysqli_query("create database mydbdemo",$con)){ //创建数据库
echo "创建数据库 mydbdemo 成功"; //成功后的提示
}else{ //如果失败
echo "创建数据库失败".mysqli_error(); //出错提示
}
mysqli_close($con); //关闭数据库连接
?>
<p>创建 fruit 数据表</p>
<?php header("Content-Type: text/html;charset=utf-8");
error_reporting(0); //禁止错误输出
$con = mysqli_connect('127.0.0.1:3306','root','root'); //创建数据库连接
if(!$con){ //如果失败
die('连接mysql数据库失败'.mysqli_error()); //显示出错误信息
}
mysqli_select_db("mydbdemo",$con); //选择需要使用的数据库
$sql = "create table fruit
(name varchar(15),
color varchar(15),
price float
)"; //使用mysqli_query执行sql语句
mysqli_query($sql,$con);
echo "<p>创建数据表 fruit 成功";
mysqli_close($con);
?>
3、增删查改sql语句
insert into fruit (name,color,price) values ('苹果','绿色','八块五');
update fruit set price='9.9' where price = '8.8';
select * from fruit;
delete from fruit where price = '8.8' ;
4、mysqli_query()执行sql语句
mysqli_query($sql);
想学习更多相关内容请访问PHP中文网:PHP视频教程
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










