登录  /  注册
首页 > 后端开发 > PHP7 > 正文

详解PHP7连接数据库以及增删查改(mysqli方法)

藏色散人
发布: 2020-11-06 15:26:10
转载
7003人浏览过

用mysqli方法 实现以下功能(php7/" target="_blank">php7):

1、连接MySQL数据库服务器;
2、创建一个名为test的数据库;
3、在该数据库内创建一个名为“testTable”的数据表,数据表至少包含三个字段,字段名字、类型和属性自定;
4、为该数据库插入三条记录,并查询该数据表的所有数据;
5、修改其中的一条记录,并查询该数据表的所有数据;
6、删除其中的一条记录,并查询该数据表的所有数据;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" >
<title>mysqli方法实现连接数据库,及增删查改</title>
</head>
<body>
<?php
	$con = @mysqli_connect("localhost","root","15118595615");
    if($con){
		echo "数据库连接成功!</br>";
	}
	else{
		echo "数据库连接失败!</br>";
	}


	$sql="CREATE DATABASE test";
	if (mysqli_query($con,$sql)){
	echo "数据库创建成功!</br>";
	}else{
	echo "数据库创建失败!</br>".mysqli_error($con)."</br>";
	}
	

	mysqli_select_db($con,"test");
	$table="CREATE TABLE testTable(
	student_id int(11) auto_increment primary key,
	student_no char(10) not null unique,
	student_name char(20) not null)";
	if(mysqli_query($con,$table)){
		echo "数据表创建成功!</br>";
	}
	else{
		echo "数据表创建失败!</br>".mysqli_error($con)."</br>";
	}
	
	$mysqli=new mysqli("localhost","root","15118595615","test");
	$query="select * from testTable";
	$insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values(&#39;null&#39;,&#39;20170001&#39;,&#39;张三&#39;)");
	mysqli_free_result($insertdatas);
	$insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values(&#39;null&#39;,&#39;20170002&#39;,&#39;李四&#39;)");
	mysqli_free_result($insertdatas);
	$insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values(&#39;null&#39;,&#39;20170003&#39;,&#39;王五&#39;)");
	if($insertdatas){
		echo "数据插入成功!</br>";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].&#39; &nbsp&#39;;
			echo $row["student_no"].&#39; &nbsp&#39;;
			echo $row["student_name"]."</br>";
		}
	}
	else{
		echo "数据插入失败!</br>".mysqli_error($con)."</br>";
	}
	mysqli_free_result($insertdatas);


	$up=mysqli_query($con,"update testTable set student_no=&#39;20180001&#39; where student_name=&#39;张三&#39;");
	if($up){
		echo "数据更新成功!</br>";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].&#39; &nbsp&#39;;
			echo $row["student_no"].&#39; &nbsp&#39;;
			echo $row["student_name"]."</br>";
		}
	}
	else{
		echo "数据更新失败!</br>".mysqli_error($con)."</br>";
	}
	mysqli_free_result($up);


	$del=mysqli_query($con,"delete from testTable where student_name=&#39;李四&#39;");
	if($del){
		echo "数据删除成功!</br>";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].&#39; &nbsp&#39;;
			echo $row["student_no"].&#39; &nbsp&#39;;
			echo $row["student_name"]."</br>";
		}
	}
	else{
		echo "数据删除失败!</br>".mysqli_error($con)."</br>";
	}
	mysqli_free_result($del);
	
	mysqli_close($con);
    
?>
</body>
</html>
登录后复制

最终效果如下:

在这里插入图片描述
写代码的时候要注意PHP7和PHP5的一些差别:
1、PHP7要将PHP5的mysql()换成mysqli()
2、PHP7的查询语句要写成mysqli( c o n n e c t , connect, connect,sql),PHP5的写法和PHP7的相反mysql( s q l , sql, sqlconnect)

温馨提示:
每次查询完之后一定要用mysqli_free_result()函数释放资源!不然会报错,无法执行下一条查询语句!初学的时候走了不少弯路,血的教训,希望能给初学的朋友帮助,少走弯路!

用mysqli方法 实现以下功能(php7):

1、连接MySQL数据库服务器;
2、创建一个名为test的数据库;
3、在该数据库内创建一个名为“testTable”的数据表,数据表至少包含三个字段,字段名字、类型和属性自定;
4、为该数据库插入三条记录,并查询该数据表的所有数据;
5、修改其中的一条记录,并查询该数据表的所有数据;
6、删除其中的一条记录,并查询该数据表的所有数据;

<!DOCTYPE html><html><head><meta charset="UTF-8" ><title>mysqli方法实现连接数据库,及增删查改</title></head><body><?php
	$con = @mysqli_connect("localhost","root","15118595615");
    if($con){
		echo "数据库连接成功!</br>";
	}
	else{
		echo "数据库连接失败!</br>";
	}


	$sql="CREATE DATABASE test";
	if (mysqli_query($con,$sql)){
	echo "数据库创建成功!</br>";
	}else{
	echo "数据库创建失败!</br>".mysqli_error($con)."</br>";
	}
	

	mysqli_select_db($con,"test");
	$table="CREATE TABLE testTable(
	student_id int(11) auto_increment primary key,
	student_no char(10) not null unique,
	student_name char(20) not null)";
	if(mysqli_query($con,$table)){
		echo "数据表创建成功!</br>";
	}
	else{
		echo "数据表创建失败!</br>".mysqli_error($con)."</br>";
	}
	
	$mysqli=new mysqli("localhost","root","15118595615","test");
	$query="select * from testTable";
	$insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values(&#39;null&#39;,&#39;20170001&#39;,&#39;张三&#39;)");
	mysqli_free_result($insertdatas);
	$insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values(&#39;null&#39;,&#39;20170002&#39;,&#39;李四&#39;)");
	mysqli_free_result($insertdatas);
	$insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values(&#39;null&#39;,&#39;20170003&#39;,&#39;王五&#39;)");
	if($insertdatas){
		echo "数据插入成功!</br>";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].&#39; &nbsp&#39;;
			echo $row["student_no"].&#39; &nbsp&#39;;
			echo $row["student_name"]."</br>";
		}
	}
	else{
		echo "数据插入失败!</br>".mysqli_error($con)."</br>";
	}
	mysqli_free_result($insertdatas);


	$up=mysqli_query($con,"update testTable set student_no=&#39;20180001&#39; where student_name=&#39;张三&#39;");
	if($up){
		echo "数据更新成功!</br>";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].&#39; &nbsp&#39;;
			echo $row["student_no"].&#39; &nbsp&#39;;
			echo $row["student_name"]."</br>";
		}
	}
	else{
		echo "数据更新失败!</br>".mysqli_error($con)."</br>";
	}
	mysqli_free_result($up);


	$del=mysqli_query($con,"delete from testTable where student_name=&#39;李四&#39;");
	if($del){
		echo "数据删除成功!</br>";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].&#39; &nbsp&#39;;
			echo $row["student_no"].&#39; &nbsp&#39;;
			echo $row["student_name"]."</br>";
		}
	}
	else{
		echo "数据删除失败!</br>".mysqli_error($con)."</br>";
	}
	mysqli_free_result($del);
	
	mysqli_close($con);
    ?></body></html>
登录后复制

最终效果如下:
在这里插入图片描述
写代码的时候要注意PHP7和PHP5的一些差别:
1、PHP7要将PHP5的mysql()换成mysqli()
2、PHP7的查询语句要写成mysqli(                                 c                         o                         n                         n                         e                         c                         t                         ,                            connect,                 connect,sql),PHP5的写法和PHP7的相反mysql(                                 s                         q                         l                         ,                            sql,                 sqlconnect)

温馨提示:
每次查询完之后一定要用mysqli_free_result()函数释放资源!不然会报错,无法执行下一条查询语句!初学的时候走了不少弯路,血的教训,希望能给初学的朋友帮助,少走弯路!

以上就是详解PHP7连接数据库以及增删查改(mysqli方法)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:csdn网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号