首頁 > 後端開發 > php教程 > 绑定列,列王的纷争账号绑定_PHP教程

绑定列,列王的纷争账号绑定_PHP教程

WBOY
發布: 2016-07-12 09:04:03
原創
910 人瀏覽過

绑定列,列王的纷争账号绑定

<?<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.htmlTechArticle绑定列,列王的纷争账号绑定 ? php try { $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd' ); $dbh -setAttribute(PDO::ATTR_ERRMODE, PDO:: ER...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板