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

用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()函数释放资源!不然会报错,无法执行下一条查询语句!初学的时候走了不少弯路,血的教训,希望能给初学的朋友帮助,少走弯路!

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

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

76c82f278ac045591c9159d381de2c57100db36a723c770d327fc0aef2ce13b193f0f5c25f18dab9d176bd4f6de5d30eb0472fae589efacfdc9167ddd7cd36f5b2386ffb911b14667cb8f0f91ea547a7mysqli方法实现连接数据库,及增删查改6e916e0f7d1e588d4f442bf645aedb2f9c3bca370b5104690d9ef395f2c5f8d16c04bd5ca3fcae76e30b72ad730ca86d59d06dc9b7a171f01fdcda2c7b5d7a7c";
	}
	else{
		echo "数据库连接失败!0b9f73f8e206867bd1f5dc5957dbcb38";
	}


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

	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 "数据表创建成功!0b9f73f8e206867bd1f5dc5957dbcb38";
	}
	else{
		echo "数据表创建失败!0b9f73f8e206867bd1f5dc5957dbcb38".mysqli_error($con)."0b9f73f8e206867bd1f5dc5957dbcb38";
	}
	
	$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('null','20170001','张三')");
	mysqli_free_result($insertdatas);
	$insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values('null','20170002','李四')");
	mysqli_free_result($insertdatas);
	$insertdatas=mysqli_query($con,"insert into testTable(student_id,student_no,student_name) values('null','20170003','王五')");
	if($insertdatas){
		echo "数据插入成功!0b9f73f8e206867bd1f5dc5957dbcb38";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].' &nbsp';
			echo $row["student_no"].' &nbsp';
			echo $row["student_name"]."0b9f73f8e206867bd1f5dc5957dbcb38";
		}
	}
	else{
		echo "数据插入失败!0b9f73f8e206867bd1f5dc5957dbcb38".mysqli_error($con)."0b9f73f8e206867bd1f5dc5957dbcb38";
	}
	mysqli_free_result($insertdatas);


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


	$del=mysqli_query($con,"delete from testTable where student_name='李四'");
	if($del){
		echo "数据删除成功!0b9f73f8e206867bd1f5dc5957dbcb38";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].' &nbsp';
			echo $row["student_no"].' &nbsp';
			echo $row["student_name"]."0b9f73f8e206867bd1f5dc5957dbcb38";
		}
	}
	else{
		echo "数据删除失败!0b9f73f8e206867bd1f5dc5957dbcb38".mysqli_error($con)."0b9f73f8e206867bd1f5dc5957dbcb38";
	}
	mysqli_free_result($del);
	
	mysqli_close($con);
    ?>36cc49f0c466276486e50c850b7e495673a6ac4ed44ffec12cee46588e518a5e

最终效果如下:
在这里插入图片描述
写代码的时候要注意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()函数释放资源!不然会报错,无法执行下一条查询语句!初学的时候走了不少弯路,血的教训,希望能给初学的朋友帮助,少走弯路!

Atas ialah kandungan terperinci 详解PHP7连接数据库以及增删查改(mysqli方法). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan
Artikel ini dikembalikan pada:csdn. Jika ada pelanggaran, sila hubungi admin@php.cn Padam

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Alat panas

Muat turun versi mac editor Atom

Muat turun versi mac editor Atom

Editor sumber terbuka yang paling popular

EditPlus versi Cina retak

EditPlus versi Cina retak

Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Pelayar Peperiksaan Selamat

Pelayar Peperiksaan Selamat

Pelayar Peperiksaan Selamat ialah persekitaran pelayar selamat untuk mengambil peperiksaan dalam talian dengan selamat. Perisian ini menukar mana-mana komputer menjadi stesen kerja yang selamat. Ia mengawal akses kepada mana-mana utiliti dan menghalang pelajar daripada menggunakan sumber yang tidak dibenarkan.

Penyesuai Pelayan SAP NetWeaver untuk Eclipse

Penyesuai Pelayan SAP NetWeaver untuk Eclipse

Integrasikan Eclipse dengan pelayan aplikasi SAP NetWeaver.