Mysqli 简明教程

MySQLi - Introduction

MySQLi 是 PHP 中 MySQL API 的一个扩展,从 PHP 5.0 开始使用。它也被称为改良的 MySQL 扩展。MySQLi 背后的动机是要利用 MySQL 4.1.3 及更高版本中可用新功能的优势。它提供了比 MySQL 扩展有许多优势。

  1. MySQL 提供面向对象接口。它提供了面向对象和过程化方法来处理数据库操作。

Object Oriented Interface

<?php
   $mysqli = mysqli_connect("localhost", "user", "password", "database-name");

   $result = mysqli_query($mysqli, "SELECT 'Welcome to MySQLi' AS _msg FROM DUAL");
   $row = mysqli_fetch_assoc($result);
   echo $row['_msg'];
?>

Procedural Approach

<?php
   $mysqli = new mysqli("localhost", "user", "password", "database-name");

   $result = $mysqli→query("SELECT 'Welcome to MySQLi' AS _msg FROM DUAL");
   $row = $result→fetch_assoc();
   echo $row['_msg'];
?>
  1. MySQLi supports prepared statments.

  2. MySQLi supports multiple statments.

  3. MySQLi supports transactions.

  4. MySQLi 提供增强的调试能力。