> php教程 > php手册 > 본문

PHP가 mysqli 데이터베이스를 조작합니다.

WBOY
풀어 주다: 2016-09-19 08:55:12
원래의
889명이 탐색했습니다.

Mysql(i) 지원은 php5.0부터 추가되었으며, 새로운 기능이 객체 형태로 추가되었습니다

개선, 다양한 기능, 고효율 및 안정성을 의미합니다

컴파일 시간 매개변수:

./configure --with-<span style="color: #008080; font-family: 'Courier new' !important;">mysql=/usr/bin/mysql_config \ <span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">使用 Mysql ClientLibrary(libmysql)构建
--with-mysqli=mysqlnd \ <span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">使用 Mysql Native Dirver 即mysqlnd
--with-pdo-<span style="color: #008080; font-family: 'Courier new' !important;">mysql=mysqlnd <span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">使用 Mysql Native Dirver 即mysqlnd
</span></span></span></span></span></span></span></span>
로그인 후 복사

저작권 문제로 인해 php5.3부터 php에서는 libmysql.dll 대신 mysqlnd를 사용하게 됩니다.
mysqlnd는 zend가 개발한 mysql 데이터베이스 드라이버로, 원본에 비해 모든 면에서 개선되었습니다.

#mysqlnd를 사용하여 컴파일

./configure --with-<span style="color: #008080; font-family: 'Courier new' !important;">mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-<span style="color: #008080; font-family: 'Courier new' !important;">mysql=mysqlnd 加上你的参数
</span></span>
로그인 후 복사

mysqli 프로시저 및 객체 메소드가 지원됩니다

mysqli에서 제공하는 세 가지 클래스:
1. Mysqli 및 연결 관련
2. MySQLi_Result 처리 결과 세트
3. mysqli_stmt 전처리 클래스

#문자셋 설정
set_charset

#문자셋 가져오기
캐릭터세트이름

데이터베이스 객체 가져오기

PHP가 mysqli 데이터베이스를 조작합니다.
<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">创建mysqli对象方式 1
//屏蔽连接产生的错误
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = <span style="color: #0000ff; font-family: 'Courier new' !important;">new mysqli('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">);

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">只能用函数来判断是否连接成功
<span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_errno<span style="font-family: 'Courier new' !important;">())
{
    <span style="color: #0000ff; font-family: 'Courier new' !important;">echo <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_error<span style="font-family: 'Courier new' !important;">();
}

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">创建mysqli对象方式 2 可以设置一些参数
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_init<span style="font-family: 'Courier new' !important;">();
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2);<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">设置超时时间
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->real_connect('127.0.0.1', 'root', '', 'test'); 
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
로그인 후 복사
PHP가 mysqli 데이터베이스를 조작합니다.

쿼리: 실패 시 false를 반환하고, select는 결과 집합 개체를 성공적으로 반환하며, 다른 쿼리는 false 대신 true를 반환합니다. 이는 SQL 실행이 성공했음을 의미합니다.

결과 집합 없음 예시

PHP가 mysqli 데이터베이스를 조작합니다.
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_init<span style="font-family: 'Courier new' !important;">();
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2);<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">设置超时时间
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->real_connect('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">);

<span style="color: #800080; font-family: 'Courier new' !important;">$sql = "insert into limove(`name`, `order`) values('aa', 11)"<span style="font-family: 'Courier new' !important;">;
<span style="color: #800080; font-family: 'Courier new' !important;">$rst = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">);

<span style="color: #800080; font-family: 'Courier new' !important;">$sql = "delete from limove where id = 221"<span style="font-family: 'Courier new' !important;">;
<span style="color: #800080; font-family: 'Courier new' !important;">$rst = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">);

<span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$rst === <span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">)
{
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">errno);
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">error);
}

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">影响条数
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">affected_rows);
<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">插入的id
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">insert_id);

ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli);
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
로그인 후 복사
PHP가 mysqli 데이터베이스를 조작합니다.

결과세트가 있습니다

PHP가 mysqli 데이터베이스를 조작합니다.
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_init<span style="font-family: 'Courier new' !important;">();
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2);<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">设置超时时间
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->real_connect('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">);

<span style="color: #800080; font-family: 'Courier new' !important;">$sql = "select * from limove as limove_as"<span style="font-family: 'Courier new' !important;">;

<span style="color: #800080; font-family: 'Courier new' !important;">$result = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">);
<span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$result === <span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">)
{
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">errno);
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">error);
}

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">行数
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">num_rows);

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">列数
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">field_count);

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">字段个数
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">field_count);

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">获取所有字段的信息
<span style="color: #800080; font-family: 'Courier new' !important;">$field_arr = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_fields();

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">移动字段的指针
// $result->field_seek(1);

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">依次获取字段的信息
<span style="color: #0000ff; font-family: 'Courier new' !important;">while(<span style="color: #800080; font-family: 'Courier new' !important;">$field = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_field())
{
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$field<span style="font-family: 'Courier new' !important;">);
}

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">移动记录指针
<span style="color: #800080; font-family: 'Courier new' !important;">$result->data_seek(1<span style="font-family: 'Courier new' !important;">);

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">一次获取所有数据
<span style="color: #800080; font-family: 'Courier new' !important;">$data = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_all(MYSQLI_ASSOC);

<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">关联数组方式获取结果集
<span style="color: #800080; font-family: 'Courier new' !important;">$data = <span style="color: #0000ff; font-family: 'Courier new' !important;">array<span style="font-family: 'Courier new' !important;">();

<span style="color: #800080; font-family: 'Courier new' !important;">$result->data_seek(0); <span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">重置指针到起始
<span style="color: #0000ff; font-family: 'Courier new' !important;">while(<span style="color: #800080; font-family: 'Courier new' !important;">$row = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_assoc())
{
    <span style="color: #800080; font-family: 'Courier new' !important;">$data[] = <span style="color: #800080; font-family: 'Courier new' !important;">$row<span style="font-family: 'Courier new' !important;">;
}

ee(<span style="color: #800080; font-family: 'Courier new' !important;">$data<span style="font-family: 'Courier new' !important;">);


<span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">free();
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->close();
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
로그인 후 복사
PHP가 mysqli 데이터베이스를 조작합니다.

一次 执行多条语句 multiquery (不推荐使用)

  无结果集,此时 affected_rows 只能获取到最后的那条影响的条数

PHP가 mysqli 데이터베이스를 조작합니다.
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_init<span style="font-family: 'Courier new' !important;">();
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2);<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">设置超时时间
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->real_connect('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">);


<span style="color: #800080; font-family: 'Courier new' !important;">$sql_arr = <span style="color: #0000ff; font-family: 'Courier new' !important;">array<span style="font-family: 'Courier new' !important;">(
    'insert into limove(id,`name`, `order`) values(null, 1, 2)',        
    'insert into limove(id,`name`, `order`) values(null, 1, 222)',        
    'delete from limove where `order` = 2',<span style="font-family: 'Courier new' !important;">        
);

<span style="color: #800080; font-family: 'Courier new' !important;">$sql = <span style="color: #008080; font-family: 'Courier new' !important;">implode(';', <span style="color: #800080; font-family: 'Courier new' !important;">$sql_arr<span style="font-family: 'Courier new' !important;">);

<span style="color: #800080; font-family: 'Courier new' !important;">$result = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->multi_query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">);
<span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$result === <span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">)
{
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">errno);
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">error);
}

<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->close();
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
로그인 후 복사
PHP가 mysqli 데이터베이스를 조작합니다.

 

有结果集

PHP가 mysqli 데이터베이스를 조작합니다.
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_init<span style="font-family: 'Courier new' !important;">();
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2);<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">设置超时时间
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->real_connect('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">);

<span style="color: #800080; font-family: 'Courier new' !important;">$sql_arr = <span style="color: #0000ff; font-family: 'Courier new' !important;">array<span style="font-family: 'Courier new' !important;">(
    'show tables',        
    'desc select * from limove',        
    'show create table limove',<span style="font-family: 'Courier new' !important;">        
);

<span style="color: #800080; font-family: 'Courier new' !important;">$sql = <span style="color: #008080; font-family: 'Courier new' !important;">implode(';', <span style="color: #800080; font-family: 'Courier new' !important;">$sql_arr<span style="font-family: 'Courier new' !important;">);

<span style="color: #800080; font-family: 'Courier new' !important;">$rst = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->multi_query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">);

<span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$rst === <span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">)
{
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">errno);
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">error);
}

<span style="color: #0000ff; font-family: 'Courier new' !important;">do<span style="font-family: 'Courier new' !important;">{
    <span style="color: #800080; font-family: 'Courier new' !important;">$result = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->store_result();<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">获取当前光标所在的结果集
    
    <span style="color: #800080; font-family: 'Courier new' !important;">$data = <span style="color: #800080; font-family: 'Courier new' !important;">$result-><span style="font-family: 'Courier new' !important;">fetch_all();
    
    ee(<span style="color: #800080; font-family: 'Courier new' !important;">$data<span style="font-family: 'Courier new' !important;">);
    
}<span style="color: #0000ff; font-family: 'Courier new' !important;">while(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->next_result());<span style="color: #008000; font-family: 'Courier new' !important;">#<span style="color: #008000; font-family: 'Courier new' !important;">光标移动到下一个结果集

<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->close();
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
로그인 후 복사
PHP가 mysqli 데이터베이스를 조작합니다.

 

事务处理:

PHP가 mysqli 데이터베이스를 조작합니다.
    <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli=<span style="color: #0000ff; font-family: 'Courier new' !important;">new mysqli("localhost", "root", "123456", "xsphpdb"<span style="font-family: 'Courier new' !important;">);

    <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">事务处理
    <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->autocommit(0<span style="font-family: 'Courier new' !important;">);

    <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">true<span style="font-family: 'Courier new' !important;">;

    <span style="color: #800080; font-family: 'Courier new' !important;">$price=50<span style="font-family: 'Courier new' !important;">;

    <span style="color: #800080; font-family: 'Courier new' !important;">$sql="update zh set ye=ye-{<span style="color: #800080; font-family: 'Courier new' !important;">$price} where name='zhangsan'"<span style="font-family: 'Courier new' !important;">;
    
    <span style="color: #800080; font-family: 'Courier new' !important;">$result=<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">);

    <span style="color: #0000ff; font-family: 'Courier new' !important;">if(!<span style="color: #800080; font-family: 'Courier new' !important;">$result<span style="font-family: 'Courier new' !important;">){
        <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">;
        <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "从张三转出失败<br>
"<span style="font-family: 'Courier new' !important;">;
    }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{
        <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->affected_rows==0<span style="font-family: 'Courier new' !important;">){
            <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">;
            <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "张三的钱没有变化"<span style="font-family: 'Courier new' !important;">;    
        }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{
            <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "从张三账号中转出成功!<br>
"<span style="font-family: 'Courier new' !important;">;
        }
    }

    <span style="color: #800080; font-family: 'Courier new' !important;">$sql="update zh set ye=ye+{<span style="color: #800080; font-family: 'Courier new' !important;">$price} where name='lisi1'"<span style="font-family: 'Courier new' !important;">;

    <span style="color: #800080; font-family: 'Courier new' !important;">$result=<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->query(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">);

    <span style="color: #0000ff; font-family: 'Courier new' !important;">if(!<span style="color: #800080; font-family: 'Courier new' !important;">$result<span style="font-family: 'Courier new' !important;">){
        <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">;
        <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "从李四转入失败<br>
"<span style="font-family: 'Courier new' !important;">;
    }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{
        <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->affected_rows==0<span style="font-family: 'Courier new' !important;">){
            <span style="color: #800080; font-family: 'Courier new' !important;">$error=<span style="color: #0000ff; font-family: 'Courier new' !important;">false<span style="font-family: 'Courier new' !important;">;
            <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "李四的钱没有变化"<span style="font-family: 'Courier new' !important;">;    
        }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{
            <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "向李四账号中转入成功!<br>
"<span style="font-family: 'Courier new' !important;">;
        }
    }

    <span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #800080; font-family: 'Courier new' !important;">$error<span style="font-family: 'Courier new' !important;">){
        <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "转账成功!"<span style="font-family: 'Courier new' !important;">;
        <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">commit();
    }<span style="color: #0000ff; font-family: 'Courier new' !important;">else<span style="font-family: 'Courier new' !important;">{
        <span style="color: #0000ff; font-family: 'Courier new' !important;">echo "转账失败!"<span style="font-family: 'Courier new' !important;">;
        <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli-><span style="font-family: 'Courier new' !important;">rollback();
    }

    <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->autocommit(1<span style="font-family: 'Courier new' !important;">);
    <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->close();
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
로그인 후 복사
PHP가 mysqli 데이터베이스를 조작합니다.

 

mysqli_stmt:mysqli预处理类(推荐):表示了准备好的一个语句,服务器端只编译一次sql
用mysqli和mysqli_result可以实现同样的功能
优点:效率高,适用于语句相同只是数据不同的情况 ,可以阻止sql注入的产生

 

 mysqli_stmt示例:非select语句

PHP가 mysqli 데이터베이스를 조작합니다.
<span style="color: #0000ff; font-family: 'Courier new' !important;">require  'fns.php'<span style="font-family: 'Courier new' !important;">;

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">创建mysqli对象方式 
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = @<span style="color: #0000ff; font-family: 'Courier new' !important;">new mysqli('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">);

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">只能用函数来判断是否连接成功
<span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_errno<span style="font-family: 'Courier new' !important;">())
{
    <span style="color: #0000ff; font-family: 'Courier new' !important;">echo <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_error<span style="font-family: 'Courier new' !important;">();
    <span style="color: #0000ff; font-family: 'Courier new' !important;">die<span style="font-family: 'Courier new' !important;">;
}

<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->set_charset('utf8'<span style="font-family: 'Courier new' !important;">);

<span style="color: #800080; font-family: 'Courier new' !important;">$sql = "insert into limove values(?, ?, ?)"; <span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">语句一样值不相同情况



<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">mysqli中有直接的方法可用
<span style="color: #800080; font-family: 'Courier new' !important;">$stmt = <span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->prepare(<span style="color: #800080; font-family: 'Courier new' !important;">$sql<span style="font-family: 'Courier new' !important;">);

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">绑定参数
<span style="color: #800080; font-family: 'Courier new' !important;">$stmt->bind_param('iss', <span style="color: #800080; font-family: 'Courier new' !important;">$id, <span style="color: #800080; font-family: 'Courier new' !important;">$name, <span style="color: #800080; font-family: 'Courier new' !important;">$order<span style="font-family: 'Courier new' !important;">);

<span style="color: #0000ff; font-family: 'Courier new' !important;">for(<span style="color: #800080; font-family: 'Courier new' !important;">$i=0;<span style="color: #800080; font-family: 'Courier new' !important;">$i$i++<span style="font-family: 'Courier new' !important;">){
    <span style="color: #800080; font-family: 'Courier new' !important;">$id = 0<span style="font-family: 'Courier new' !important;">;
    <span style="color: #800080; font-family: 'Courier new' !important;">$name = 'name'<span style="font-family: 'Courier new' !important;">;
    <span style="color: #800080; font-family: 'Courier new' !important;">$order = <span style="color: #008080; font-family: 'Courier new' !important;">mt_rand(1, 1000<span style="font-family: 'Courier new' !important;">);
    <span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">execute();

}

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">最后id
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">insert_id);

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">影响的行数 注:最后一条执行的
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">affected_rows);

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">错误号
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">errno);

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">错误信息
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt-><span style="font-family: 'Courier new' !important;">error);

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">stmt对象中可以看到更多的信息
ee(<span style="color: #800080; font-family: 'Courier new' !important;">$stmt<span style="font-family: 'Courier new' !important;">);

eee(<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli);
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
로그인 후 복사
PHP가 mysqli 데이터베이스를 조작합니다.

 

  mysqli_stmt示例:select语句 1 

PHP가 mysqli 데이터베이스를 조작합니다.
<span style="color: #0000ff; font-family: 'Courier new' !important;">require  'fns.php'<span style="font-family: 'Courier new' !important;">;

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">创建mysqli对象方式 
<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli = @<span style="color: #0000ff; font-family: 'Courier new' !important;">new mysqli('127.0.0.1', 'root', '', 'test'<span style="font-family: 'Courier new' !important;">);

<span style="color: #008000; font-family: 'Courier new' !important;">//<span style="color: #008000; font-family: 'Courier new' !important;">只能用函数来判断是否连接成功
<span style="color: #0000ff; font-family: 'Courier new' !important;">if(<span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_errno<span style="font-family: 'Courier new' !important;">())
{
    <span style="color: #0000ff; font-family: 'Courier new' !important;">echo <span style="color: #008080; font-family: 'Courier new' !important;">mysqli_connect_error<span style="font-family: 'Courier new' !important;">();
    <span style="color: #0000ff; font-family: 'Courier new' !important;">die<span style="font-family: 'Courier new' !important;">;
}

<span style="color: #800080; font-family: 'Courier new' !important;">$mysqli->set_charset('utf8'<span style="font-family: 'Courier new' !important;">);

<span style="color: #800080; font-family: 'Courier new' !important;">$sql = "select * from limove where id							
		</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿