<?php
header('content-Type:text/html;charset=utf-8');//设置页面编码,如果文件是gbk编码,则charset也应用gbk
function connect() {
$link = mysqli_connect("localhost", "root", "123456", "employees");//面向过程 $link = new mysqli("localhost", "root", "123456", "employees");//面向对象
$link->query("SET NAMES utf8");//执行语句
$link->set_charset('utf8');//面向对象
return $link;
}
$link = connect();
//print_r($link);
$sql = 'select * from employees limit 10';
$result = $link->query($sql);//面向对象
//print_r($result);
//fetch_all、fetch_array、fetch_assoc与fetch_row
$result->data_seek(0);
$data = $result->fetch_all();
//fetch_array获取一条数据
$result->data_seek(0);
$data = $result->fetch_array(MYSQLI_NUM);
//fetch_assoc获取多条数据 有字段名
$result->data_seek(0);//0 为重置指针到起始
$data = [];
$data = $result->fetch_assoc();
//fetch_row获取一条数据
$result->data_seek(0);
$data = $result->fetch_row();
//获取字段信息 返回是stdClass对象
$fields = $result->fetch_fields();
$result->field_seek(1);
$fields = $result->fetch_field();
$result->data_seek(0);
//fetch_object 获取stdClass没有table等字段
$data = $result->fetch_object();
//fetch_field_direct
$data = $result->fetch_field_direct(1);
//$sql = "insert into employees(`birth_date`,`first_name`,`last_name`,`gender`,`hire_date`) values ('1988-10-9', 'pw', 'strick', 'M', '1988-10-9')";
//$result = $link->query($sql);
//var_dump('affected_rows受影响行数');
//var_dump($link->affected_rows);//受影响行数
//var_dump('insert_id主键');
//var_dump($link->insert_id);//主键
//print_r(mysqli_error($link));
//预处理
//添加
/* $sql = "insert into employees(`birth_date`,`first_name`,`last_name`,`gender`,`hire_date`) values (?,?,?,?,?)";
$stmt = $link->prepare($sql);//获得预处理对象
$birth_date = '1989-10-9';//必须得用变量赋值
$first_name = 'pw2';
$last_name = 'strick2';
$gender = 'M';
$hire_date = '1988-10-9';
$stmt->bind_param('sssss', $birth_date, $first_name, $last_name, $gender, $hire_date);//绑定时使用变量绑定
$stmt->execute();//执行预处理
var_dump('insert_id主键');
var_dump($stmt->insert_id);
var_dump('affected_rows受影响行数');
var_dump($stmt->affected_rows); */
//查询
$sql = 'select * from employees limit ?';
$stmt = $link->prepare($sql);
$count = 10;
$stmt->bind_param("i", $count);
$stmt->execute();
$result = $stmt->get_result();//结果集取后的操作就和之前一样了
PD9waHAKCmhlYWRlcignY29udGVudC1UeXBlOnRleHQvaHRtbDtjaGFyc2V0PXV0Zi04Jyk7Ly/orr7nva7pobXpnaLnvJbnoIHvvIzlpoLmnpzmlofku7bmmK9nYmvnvJbnoIHvvIzliJljaGFyc2V05Lmf5bqU55SoZ2JrCgoKCmZ1bmN0aW9uIGNvbm5lY3QoKSB7CgkkbGluayA9IG15c3FsaV9jb25uZWN0KCJsb2NhbGhvc3QiLCAicm9vdCIsICIxMjM0NTYiLCAiZW1wbG95ZWVzIik7Ly/pnaLlkJHov4fnqIsKCSRsaW5rID0gbmV3IG15c3FsaSgibG9jYWxob3N0IiwgInJvb3QiLCAiMTIzNDU2IiwgImVtcGxveWVlcyIpOy8v6Z2i5ZCR5a+56LGhCgoJJGxpbmstPnF1ZXJ5KCJTRVQgTkFNRVMgdXRmOCIpOy8v5omn6KGM6K+t5Y+lCgkKCW15c3FsaV9zZXRfY2hhcnNldCgkbGluaywgJ3V0ZjgnKTsvL+mdouWQkei/h+eoiwoJJGxpbmstPnNldF9jaGFyc2V0KCd1dGY4Jyk7Ly/pnaLlkJHlr7nosaEKCQoJcmV0dXJuICRsaW5rOwp9CgokbGluayA9IGNvbm5lY3QoKTsKCi8vcHJpbnRfcigkbGluayk7CiRzcWwgPSAnc2VsZWN0ICogZnJvbSBlbXBsb3llZXMgbGltaXQgMTAnOwokcmVzdWx0ID0gbXlzcWxpX3F1ZXJ5KCRsaW5rLCAkc3FsKTsvL+mdouWQkei/h+eoiwokcmVzdWx0ID0gJGxpbmstPnF1ZXJ5KCRzcWwpOy8v6Z2i5ZCR5a+56LGhCi8vcHJpbnRfcigkcmVzdWx0KTsKCgoKLy9mZXRjaF9hbGzjgIFmZXRjaF9hcnJheeOAgWZldGNoX2Fzc29j5LiOZmV0Y2hfcm93Ci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