duafagm1066 2017-08-30 15:47
浏览 72
已采纳

将PHP示例代码从mysqli转换为PDO

I hope that someone would be willing to help me out here - since I know it shouldn't be too hard.

I found and tutorial example of something that I wish to do. Unfortunately the sample code make use of the old MYSQLi and I have no idea how to go about to change it over to PDO.

Here is a link to the full tutorial

 $connect = mysqli_connect("localhost", "root", "", "testing");  
 $query = "SELECT * FROM tbl_employee ORDER BY id desc";  
 $result = mysqli_query($connect, $query);

Then further down:

       <div class="container" style="width:800px;">  
            <h2 align="center">PHP AJAX Jquery - Load Dynamic Content in Bootstrap Popover</h2>  
            <h3 align="center">Employee Data</h3>                 
            <br /><br />  
            <div class="table-responsive">  
                 <table class="table table-bordered">  
                      <tr>  
                           <th width="20%">ID</th>  
                           <th width="80%">Name</th>  
                      </tr>  
                      <?php  
                      while($row = mysqli_fetch_array($result))  
                      {  
                      ?>  
                      <tr>  
                           <td><?php echo $row["id"]; ?></td>  
                           <td><a href="#" class="hover" id="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></a></td>  
                      </tr>  
                      <?php  
                      }  
                      ?>  
                 </table>  
            </div>  
       </div>  
  • 写回答

1条回答 默认 最新

  • dongshen4129 2017-08-30 15:53
    关注

    This is how you would do this query in PDO

    database connection:

    $host = 'localhost';
    $db   = '';
    $user = '';
    $pass = '';
    $charset = 'utf8';
    
    $dsn = "mysql:host={$host};dbname={$db};charset={$charset}";
    $opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    $pdo = new PDO($dsn, $user, $pass, $opt);
    

    Notice we set some extra parameters here, namely ATTR_EMULATE_PREPARES, which makes allows PDO to do proper prepared statements instead of emulated ones, which makes it safer. (If we didn't do this, it would basically be just as safe as string concatenation) Note, in your query you wrote, this actually doesn't matter much as there is nothing to prepare, but it will be useful later when you have some statements that needs data to be passed.

    Query:

    $stmt = $pdo->query("SELECT * FROM tbl_employee ORDER BY id desc")->fetchAll();
    

    Loop:

    if(!empty($stmt)) {
        foreach($stmt as $row) {  
            ?>  
                <tr>  
                   <td><?php echo $row["id"]; ?></td>  
                   <td><a href="#" class="hover" id="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></a></td>  
                </tr>
            <?
        }
    } else {
        //show error, no results
    }
    

    I would like to mention that the MySQLi_* interface is not really "old", it's not deprecated or unused by far. I prefer PDO over MySQLi because it seems easier to me, but both syntax's are fully valid and supported. The problem arises when you are not using Prepared Statements using either interface.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?