MySQLi
#连接数据库
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
#选择数据库
mysqli_select_db($conn, 'test1');
#发送命令
$retval = mysqli_query($conn,$sql);
#关闭连接
mysqli_close($conn);
$retval = mysqli_query($conn, $sql);
#查询表每列值
$row = mysqli_fetch_array($retval, MYSQLI_ASSOC)
$row['name']
$row = mysqli_fetch_array($retval, MYSQLI_NUM)
$row[0]
mysqli_num_rows($result) > 0
$row = mysqli_fetch_assoc($retval)
$row['name']
#查询完释放内存
mysqli_free_result($retval);
//执行多条语句
//创建 SQL 语句模板并发送到数据库。预留的值使用参数 "?" 标记
$sql = "INSERT INTO MyGuests(firstname, lastname, email) VALUES(?, ?, ?)";
// 为 mysqli_stmt_prepare() 初始化 statement 对象
$stmt = mysqli_stmt_init($conn);
//预处理语句
if (mysqli_stmt_prepare($stmt, $sql)) {
// 绑定参数
mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
// "sss" 告诉mysql参数是字符串
//i - 整数
//d - 双精度浮点数
//s - 字符串
//b - 布尔值
// 设置参数并执行
$firstname = 'John';
$lastname = 'Doe';
$email = 'john@example.com';
mysqli_stmt_execute($stmt);
$firstname = 'Mary';
$lastname = 'Moe';
$email = 'mary@example.com';
mysqli_stmt_execute($stmt);
}