首頁 > 後端開發 > PHP7 > 主體

詳解PHP7連接資料庫以及增刪查改(mysqli方法)

藏色散人
發布: 2023-02-17 18:00:02
轉載
7063 人瀏覽過

用mysqli方法實作下列函數(php7):

1、連接MySQL資料庫伺服器;
2、建立一個名為test的資料庫;
3、在該資料庫內建立一個名為「testTable」的資料表,資料表至少包含三個字段,欄位名字、類型和屬性自定;
4、為該資料庫插入三筆記錄,並查詢該資料表的所有資料;
5、修改其中的一筆記錄,並查詢該資料表的所有資料;
6、刪除其中的一筆記錄,並查詢該資料表的所有資料;





mysqli方法实现连接数据库,及增删查改

"; } 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"].'  '; echo $row["student_no"].'  '; 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"].'  '; echo $row["student_no"].'  '; 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"].'  '; echo $row["student_no"].'  '; 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,             sq#lconnect);

溫馨提示:
每次查詢完後一定要用mysqli_free_result()函數釋放資源!不然會報錯,無法執行下一查詢語句!初學的時候走了不少彎路,血的教訓,希望能給初學的朋友幫助,少走彎路!

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

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

mysqli方法实现连接数据库,及增删查改
"; } 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"].'  '; echo $row["student_no"].'  '; 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"].'  '; echo $row["student_no"].'  '; 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"].'  '; echo $row["student_no"].'  '; 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
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡[email protected]
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!