>백엔드 개발 >PHP 튜토리얼 >php 操作 mysql 预处理(未完待续)_PHP教程

php 操作 mysql 预处理(未完待续)_PHP教程

WBOY
WBOY원래의
2016-07-20 11:14:31982검색

<span 由于本地 PHP 版本过底,没办法进行完整的测试<br /><br />$host</span> = 'localhost'<span ;
</span><span $user</span> = 'root'<span ;
</span><span $password</span> = ''<span ;
</span><span $dbname</span> = 'cmstop'<span ;
</span><span $mysqli</span> = <span new</span> mysqli(<span $host</span>, <span $user</span>, <span $password</span>, <span $dbname</span>) or <span exit</span>('无法连接到mysql:'.<span $mysqli</span>-><span connect_error);

 </span><span $sql</span>  = "SELECT CURRENT_USER(); SELECT * FROM `db_setting` LIMIT 5;"<span ;

</span><span if</span> (<span $mysqli</span>->multi_query(<span $sql</span>)){<span //</span><span 多条查询</span>
    <span do</span><span {
        </span><span if</span>(<span $result</span> = <span $mysqli</span>->store_result()){<span //</span><span 如果还有还结果集,取当前的一个结果集返回</span>
            <span while</span>(<span $row</span> = <span $result</span>->fetch_row()){<span //</span><span 取一条记录</span>
                <span echo</span> <span $row</span>[0<span ];
            }
            </span><span $result</span>->free();<span //</span><span 当此结果集中无记录时,释放</span>
<span         }
        </span><span if</span>(<span $mysqli</span>->more_results()){<span //</span><span 如果还有一个结果集,则输出分隔线</span>
            <span echo</span> "<hr />"<span ;
        }
    }</span><span while</span>(<span $mysqli</span>-><span next_result());
} 


</span><span //</span><span 预处理</span>
<span $sql</span> = "SELECT `app`,`var` FROM `db_setting` WHERE `app`=? LIMIT 5"<span ;
</span><span $app</span> = 'article'<span ;
</span><span $stmt</span> = <span $mysqli</span>-><span stmt_init();
</span><span if</span>(<span $stmt</span>->prepare(<span $sql</span><span )) {
    </span><span $stmt</span>->bind_param("s", <span $app</span><span );
    </span><span $stmt</span>->execute();<span //</span><span 执行查询</span>
    
    <span $stmt</span>->bind_result(<span $field1</span>, <span $field2</span>); <span //</span><span 生成两个字段的结果集</span>
    <span while</span> (<span $stmt</span>-><span fetch()) {
        </span><span echo</span> '<br/>', <span $field1</span>,' => ', <span $field2</span><span ;
    }
    </span><span echo</span>'<hr/>'<span ;
    
    </span><span $app</span> = 'guestbook'<span ;
    </span><span $stmt</span>->execute();<span //</span><span 执行查询</span>
    <span $result</span> = <span $stmt</span>->get_result(); <span //</span><span 生成两个字段的结果集</span>
    <span while</span> (<span $row</span> = <span $result</span>-><span fetch_array(MYSQLI_NUM)){
        </span><span echo</span> <span $row</span>[0],' => ',<span $row</span>[1<span ];
    }
    
    </span><span $stmt</span>-><span close();
}
</span><span $mysqli</span>->close();

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440275.htmlTechArticle$host = 'localhost' = 'root' = '' = 'cmstop' = mysqli( , , , ) or ('无法连接到mysql:'. - = "SELECT CURRENT_USER(); SELECT * FROM `db_setting` LIMIT 5;" ( -multi_query( )){ ( =...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.