>  기사  >  백엔드 개발  >  PHP7 데이터베이스 접속 및 추가, 삭제, 조회, 수정에 대한 자세한 설명(mysqli 방식)

PHP7 데이터베이스 접속 및 추가, 삭제, 조회, 수정에 대한 자세한 설명(mysqli 방식)

藏色散人
藏色散人앞으로
2020-11-06 15:26:107205검색

다음 기능을 달성하려면 mysqli 메서드를 사용하세요(php7).

1. MySQL 데이터베이스 서버에 연결합니다.
2. test라는 데이터베이스를 만듭니다. 데이터베이스 "testTable"이라는 데이터 테이블. 필드 이름, 유형 및 속성이 사용자 정의됩니다.
4. 데이터베이스에 세 개의 레코드를 삽입하고 데이터 테이블의 모든 데이터를 쿼리합니다. . 레코드 중 하나를 수정하고 데이터 테이블의 모든 데이터를 쿼리합니다.
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의 차이점: PHP7 데이터베이스 접속 및 추가, 삭제, 조회, 수정에 대한 자세한 설명(mysqli 방식) 1. PHP7은 PHP5의
mysql()
mysqli()로 바꿔야 합니다. 2. PHP7의 쿼리 문은 mysqli(
)로 작성해야 합니다. ​ ​​ ​​                   기음                                     영형                                     N                                     N                                     이자형                                     기음                                     티                                     , ,                   ​​ ​​ 연결하다, ​​ connect,sql) , PHP5의 작성 방법 PHP7mysql과 반대입니다. ( ​ ​​ ​​                   에스                                     q q                                     엘                                     ,                   ​​ ​​ ​ ​ ​ SQL, ​ ​​ ㅋㅋㅋ ult() 함수는 리소스를 해제합니다! 그렇지 않으면 오류가 보고되고 다음 쿼리 문을 실행할 수 없습니다! 저는 초보자였을 때 우회를 많이 했고, 어려운 교훈을 얻었습니다. 초보 친구들이 우회를 피하는 데 도움이 되기를 바랍니다!

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

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

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


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

	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 "数据表创建成功!";
	}
	else{
		echo "数据表创建失败!".mysqli_error($con)."";
	}
	
	$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 "数据插入成功!";
		$result=$mysqli->query($query);
		foreach($result as $row){
			echo $row["student_id"].' &nbsp';
			echo $row["student_no"].' &nbsp';
			echo $row["student_name"]."";
		}
	}
	else{
		echo "数据插入失败!".mysqli_error($con)."";
	}
	mysqli_free_result($insertdatas);


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


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

最终效果如下:
PHP7 데이터베이스 접속 및 추가, 삭제, 조회, 수정에 대한 자세한 설명(mysqli 방식)
写代码的时候要注意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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 csdn.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제