PHP使用mysqli扩展库实现增删改查(面向对象版)
mysqli扩展库是mysql扩展库的改进版本,在mysql扩展库的基础上提高了稳定性和效率,mysqli扩展库有两套东西,一套就是面向过程的mysqli另一套是面向对象的mysqli。操作方式大体和mysql扩展库大体一致,这次还是先抽取出来一个操作mysql的工具类,和调用的类。
1.mysqli扩展库操作数据库工具类
<span style="color: #000000;">php </span><span style="color: #008000;">//</span><span style="color: #008000;">数据库操作类</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> DBUtil{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$host</span>="localhost"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$username</span>="root"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$password</span>="123456"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$dbname</span>="student"<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$conn</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> DBUtil(){ </span><span style="color: #800080;">$this</span>->conn=<span style="color: #0000ff;">new</span> mysqli(<span style="color: #800080;">$this</span>->host, <span style="color: #800080;">$this</span>->username, <span style="color: #800080;">$this</span>->password,<span style="color: #800080;">$this</span>->dbname) or <span style="color: #0000ff;">die</span>(<span style="color: #800080;">$this</span>->conn-><span style="color: #000000;">connect_error); } </span><span style="color: #008000;">//</span><span style="color: #008000;">查询</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> query(<span style="color: #800080;">$sql</span><span style="color: #000000;">){ </span><span style="color: #800080;">$all</span>= <span style="color: #800080;">$this</span>->conn->query(<span style="color: #800080;">$sql</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$all</span><span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;">插入,修改,删除</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> otherOperate(<span style="color: #800080;">$sql</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->conn->query(<span style="color: #800080;">$sql</span><span style="color: #000000;">)){ </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->conn->affected_rows>0<span style="color: #000000;">){ </span><span style="color: #0000ff;">return</span> "OK"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">return</span> "ERROOR"<span style="color: #000000;">; } } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> close(){ </span><span style="color: #800080;">$this</span>->conn-><span style="color: #000000;">close(); } } </span>?>
Nach dem Login kopieren
2.下面是具体的调用工具类的代码
<span style="color: #000000;">php </span><span style="color: #0000ff;">require_once</span> "MySQLUtil.php"<span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">$sql="select * from m_student"; $util=new DBUtil(); $result=$util->query($sql); while($row=$result->fetch_assoc()){ echo "$row[stuName]".""; } $result->free(); $util->close();</span><span style="color: #008000;">*/</span> <span style="color: #800080;">$sql</span>="update m_student set stuName='杨幂' where id=3"<span style="color: #000000;">; </span><span style="color: #800080;">$util</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> DBUtil(); </span><span style="color: #800080;">$result</span>=<span style="color: #800080;">$util</span>->otherOperate(<span style="color: #800080;">$sql</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result</span><span style="color: #000000;">; </span><span style="color: #800080;">$util</span>-><span style="color: #000000;">close(); </span>?>
Nach dem Login kopieren
参考阅读:www.manongjc.com/article/1206.html
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel
R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vor
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vor
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle -Lösung
2 Wochen vor
By DDD
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
4 Wochen vor
By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen
CakePHP-Tutorial
1376
52

