MySqli擴充庫的對象對象屬性特點以及與mysql庫面向過程的比較,mysqlimysql

來源:互聯網
上載者:User

MySqli擴充庫的對象對象屬性特點以及與mysql庫面向過程的比較,mysqlimysql

在學習完mysql庫來操作mysql的方式,這是一種面向過程的方式,但是,物件導向是大勢所趨,mysqli擴充庫也就有必須學習的必要了,mysqli作為一個類庫,在我們使用的時候都是以物件導向的方式進行操作,所以,這種方案是比較好的,為此,我特意做了它與mysqli擴充庫的對應比較:

下面是mysql庫的串連,查詢語句,返回結果,釋放資源的過程:

<pre name="code" class="php"><pre name="code" class="php"><pre name="code" class="php"><pre name="code" class="php"><?php  //1:串連資料庫      $con=mysql_connect("localhost","root","toor");      //如果沒有串連成功要報錯      if(!$con){          echo "串連失敗";          exit();      }  //2: 選擇要操作的資料庫      mysql_select_db("test");  //3:發送SQL指令      mysql_query("set names utf8");//設定查詢編碼      $sql="select *from test1";      $res=mysql_query($sql,$con);  //4:返回結果(按行遍曆返回)      while($row=mysql_fetch_row($res)){          echo "$row[0]-------------$row[1]-----------$row[2]-----------$row[3]-----------$row[4]".'<br/>';      }  //5:釋放資源,關閉串連      mysql_free_result($res);      mysql_close($con);  ?>  



下面是mysqli擴充庫的串連,查詢語句,返回結果,釋放資源的過程:
<pre name="code" class="php"><?php//建立mysqli對象,執行個體化$mysqli= new MySQLi("localhost","root","toor","test");if($mysqli->connect_error){die("串連失敗 錯誤資訊:".$mysqli->connect_error);}else{echo "串連成功<br/>";}//操作資料庫,發送sql$sql="select * from test.test1";$res=$mysqli->query($sql);//返回結果while($row=$res->fetch_row()){foreach($row as $key=>$value){echo $value." ";}echo "<br/>";}//var_dump($res);//關閉資源$res->free();//關閉串連$mysqli->close();?>

可以清晰的看出來,物件導向的mysqli不僅僅在思想上更進一步,而且在代碼的複雜度上也是相對簡單的,因此,我覺得,在學習完mysql的那套庫之後,再學習mysqli擴充庫是非常必要的

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.