ext/mysqli是PHP5和MySQL4.1起ext/mysql的替换器,i
意思指的是:improved, interface, ingenious, incompatible or incomplete.
其主要有以下特点为:
- Easier maintainability The ext/mysql code base has become somewhat complex and messy. Major enhancements in the functionality of MySQL required that various feature sets be enabled or disabled based on the version of the client library. Other issues required that some features be disabled for particular operating systems.
- Better compatibility The extension needed to conform more closely to the MySQL client library, so that future enhancements to the library could be supported more easily in PHP
- Backwards compatibility Although the compatibility between ext/mysql and ext/mysqli is not perfect, effort was made to simplify porting applications from ext/mysql to ext/mysqli.
其除了类似ext/mysql中的函数,如:mysqli_query,mysqli_fetch_assoc,mysqli_close等,还支持Prepared Statements。采用Prepared Statements,可以使查询更为安全,更高效。对于大量的查询语句来说,效率确实要高得多,但是如果只是一两句查询的话,就体现不出来了。
下面是一个Prepared Statements的例子(来自PHP手册):
- $query = “INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)”;
- $stmt = $mysqli->prepare($query);
- $stmt->bind_param(“sss”, $val1, $val2, $val3);
- $val1 = ‘Stuttgart’;
- $val2 = ‘DEU’;
- $val3 = ‘Baden-Wuerttemberg’;
- /* Execute the statement */
- $stmt->execute();
- $val1 = ‘Bordeaux’;
- $val2 = ‘FRA’;
- $val3 = ‘Aquitaine’;
- /* Execute the statement */
- $stmt->execute();
- /* close statement */
- $stmt->close();
看语法跟perl的DBI有些相似……