mysqli 확장 라이브러리는 mysql 확장 라이브러리의 향상된 버전입니다. mysql 확장 라이브러리를 기반으로 안정성과 효율성을 향상시켰습니다. 하나는 프로세스 지향 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>?>
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]"."</br>"; } $result->free(); $util->close();<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>?>
참고자료 : www.manongjc.com/article/1206.html