首先, mysqli 连接是永久连接,而mysql是非永久连接。
mysql连接每当第二次使用的时候,都会重新打开一个新的进程,而mysqli则只使用同一个进程,这样可以很大程度的减轻服务器端压力。
其次,mysqli封装了诸如事务等一些高级操作,同时封装了DB操作过程中的很多可用的方法。
应用比较多的地方是 mysqli的事务。
例子:
<span 1</span> <span $mysqli</span> = <span new</span> mysqli('localhost','root','','DB_Lib2Test'<span ); </span><span 2</span> <span $mysqli</span>->autocommit(<span false</span>);<span //</span><span 开始事务</span> <span 3</span> <span $mysqli</span>->query(<span $sql1</span><span ); </span><span 4</span> <span $mysqli</span>->query(<span $sql2</span><span ); </span><span 5</span> <span if</span>(!<span $mysqli</span>-><span errno){ </span><span 6</span> <span $mysqli</span>-><span commit(); </span><span 7</span> <span echo</span> 'ok'<span ; </span><span 8</span> }<span else</span><span { </span><span 9</span> <span echo</span> 'err'<span ; </span><span 10</span> <span $mysqli</span>-><span rollback(); </span><span 11</span> } <span //</span><span by www.jbxue.com</span>