源码库详解MySQLi函数库
2024-07-24 12:40:33
供稿:网友
版本支持: PHP > 5.0 MySQL > 4.0 2. 连接数据库: $db = new mysqli(host,user,password,database); 这是对象导向的连接方法,还有一种过程导向的连接方法。前者返回一个对象,后者返回一个资源。 $db = mysqli_connect(host,user,password,database); 3. 连接出错函数: mysqli_connect_error(); 如果连接出错,该函数返回一个错误号,否则返回0; 4. 选择数据库: $db->select_db(dbname); or mysqli_select_db(db_resource,db_name); 5. 执行查询 $result = $db->query($query); or $result = mysqli_query($db,$query); 6. 返回结果的行数: $num_results = $result->num_rows; or $num_results = mysqli_num_rows($result); 7. 取出每一行的结果(返回关系型数组): $row = $result->fetch_assoc(); or $row = mysqli_fetch_assoc($result); 8. 取出每一行的结果(返回计数型数组): $row = $result->fetch_row($result); or $row = mysqli_fetch_row($result); 9. 取出每一行的结果(返回一个对象): $row = $result->fetch_object(); or $row = mysqli_fetch_object($result); 10. 释放查询结果: $result->free(); or mysqli_free_result($result); 11. 关闭数据库连接: $db->close(); or mysqli_close($db); 12. 一条查询所影响的行数: $db->affected_rows; or mysqli_affected_rows($result); 13. 模式化SQL语句执行: $query = “insert into books values(?, ?, ?, ?)”; $stmt = $db->prepare($query); $stmt->bind_param(“sssd”, $isbn, $author, $title, $price); $stmt->execute(); echo $stmt->affected_rows.’ book inserted into database.’; $stmt->close(); 在过程化执行方式中, $db->prepare()对应mysqli_stmt_prepare()函数; $stmt->bind_param()对应mysqli_stmt_bind_param()函数; $stmt->execute() 对应mysqli_stmt_execute()函数;