Home >Backend Development >PHP Tutorial >Bind columns, Clash of Kings account binding_PHP tutorial

Bind columns, Clash of Kings account binding_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:04:03905browse

Bind column, Clash of Kings account binding

<?<span>php
    </span><span>try</span><span>{
        </span><span>$dbh</span> = <span>new</span> PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'<span>);
        </span><span>$dbh</span>->setAttribute(PDO::ATTR_ERRMODE, PDO::<span>ERRMODE_EXCEPTION);
    }</span><span>catch</span>(PDOException <span>$e</span><span>){
        </span><span>echo</span> '数据库连接失败:'.<span>$e</span>-><span>getMessage();
        </span><span>exit</span><span>;
    }
  
    </span><span>//</span><span>声明一个SELECT查询,从表contactInfo中获取D01部门的四个字段的信息</span>
    <span>$query</span> = "SELECT uid, name, phone, email FROM contactInfo WHERE departmentId='D01'"<span>;
    </span><span>try</span><span> {
        </span><span>$stmt</span> = <span>$dbh</span>->prepare(<span>$query</span>);                      <span>//</span><span>准备声明好的一个查询</span>
        <span>$stmt</span>->execute();                           <span>//</span><span>执行准备好的查询</span>
        <span>$stmt</span>->bindColumn(1, <span>$uid</span>);                    <span>//</span><span>通过列位置偏移数绑定变量$uid</span>
        <span>$stmt</span>->bindColumn(2, <span>$name</span>);                              <span>//</span><span>通过列位置偏移数绑定变量$name</span>
        <span>$stmt</span>->bindColumn('phone', <span>$phone</span>);                        <span>//</span><span>绑定列名称到变量$phone上</span>
        <span>$stmt</span>->bindColumn('email', <span>$email</span>);                 <span>//</span><span>绑定列名称到变量$email上</span>

        <span>while</span> (<span>$stmt</span>->fetch(PDO::FETCH_BOUND)) {                <span>//</span><span>fetch()方法传入特定的参数遍历</span>
            <span>echo</span> <span>$uid</span>."\t".<span>$name</span>."\t".<span>$phone</span>."\t".<span>$email</span>."\n";    <span>//</span><span>输出自动将列值赋给对应变量的值</span>
<span>        }
    } </span><span>catch</span> (PDOException <span>$e</span><span>) {
        </span><span>echo</span> <span>$e</span>-><span>getMessage();
    }</span>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1075861.htmlTechArticleBind columns, bind Clash of Kings accounts? php try { $dbh = new PDO('mysql: dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd' ); $dbh -setAttribute(PDO::ATTR_ERRMODE, PDO:: ER...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn