版本支持:PHP > 5.0 MySQL > 4.0
2. mysqli 连接数据库:
$db = new mysqli(host,user,password,database);返回一个对象
$db = mysqli_connect(host,user,password,database);返回一个资源。
3.mysqli 连接出错函数:
mysqli_connect_error();无参数,如果连接出错,该函数返回一个错误号,否则返回0;
4.选择数据库:
$db->select_db(dbname);
omysqli_select_db(db_resource,db_name);
5.mysqli 执行查询
$result = $db->query($query);
$result = mysqli_query($db,$query);
6.返回结果的行数:
$num_results = $result->num_rows;
$num_results = mysqli_num_rows($result);
7.取出每一行的结果(返回关系型数组):
$row = $result->fetch_assoc();
$row = mysqli_fetch_assoc($result);
8.取出每一行的结果(返回计数型数组):
$row = $result->fetch_row($result);
$row = mysqli_fetch_row($result);
9.取出每一行的结果(返回一个对象):
$row = $result->fetch_object();
$row = mysqli_fetch_object($result);
10.释放查询结果:
$result->free();
mysqli_free_result($result);
11.关闭数据库连接:
$db->close();
mysqli_close($db);
12.一条查询所影响的行数:
$db->affected_rows;
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()函数;
本文来自: 老吧网(www.lao8.org) 详细出处参考:http://www.lao8.org/html/8/2011-2-22/MySQLi.html
记忆的时候,可以选择一种记忆,另一种做参考。