douwen5833 2017-06-29 10:43
浏览 74
已采纳

mysql_到pdo:我做得对吗?

(browseroutput.jpg)

I just switched a old mysql_* from a old tutorial to PDO and wanted to know if im doing it right. I did'nt get the mysql_* and PDO, are they drivers or just different variants to fetch data?

My code work as it should but im kinda sceptical that it work because im a beginner.

  <?php
  // New PDO variant   

  try {
     $user = "user";
     $pass = "";

     $pdo = new PDO('mysql:host=localhost;dbname=testdb', $user, $pass);

     //build query
     $age = $_GET['age'];
     $sex = $_GET['sex'];
     $wpm = $_GET['wpm'];

     $query = "SELECT * FROM ajax_example WHERE sex = '$sex'";

     if(is_numeric($age))
     $query .= " AND age <= $age";

     if(is_numeric($wpm))
     $query .= " AND wpm <= $wpm";

     $stmt = $pdo->prepare($query);

     $display_string = "<table>";
     $display_string .= "<tr>";
     $display_string .= "<th>Name</th>";
     $display_string .= "<th>Age</th>";
     $display_string .= "<th>Sex</th>";
     $display_string .= "<th>WPM</th>";
     $display_string .= "</tr>";

     $stmt->execute(array('name' => $name));

     foreach ($stmt as $row) {
        $display_string .= "<tr>";
        $display_string .= "<td>$row[name]</td>";
        $display_string .= "<td>$row[age]</td>";
        $display_string .= "<td>$row[sex]</td>";
        $display_string .= "<td>$row[wpm]</td>";
        $display_string .= "</tr>";
     }

     echo "Query: " . $query . "<br />";

     $display_string .= "</table>";
     echo $display_string;
     $dbh = null;

  } catch (PDOException $e) {
     print "Error!: " . $e->getMessage() . "<br/>";
     die();
  }
  ?>
  • 写回答

2条回答 默认 最新

  • duanne9313 2017-06-29 13:55
    关注

    You'we almost correct, you just missed the point of prepare()

    <?php
    // New PDO variant   
    try {
        $user = "user";
        $pass = "";
    
        $pdo = new PDO('mysql:host=localhost;dbname=testdb', $user, $pass);
    
        //build query
        $age = intval($_GET['age']);
        $sex = $_GET['sex'];
        $wpm = intval($_GET['wpm']);
    
        $query = "SELECT * FROM ajax_example WHERE sex = ? AND age <= ? AND wpm <= ?";
        $stmt  = $pdo->prepare($query);
    
        $stmt->execute(array($sex,$age,$wpm));
    
        $results = $stmt->fetchall();
        if (count($results > 0)) {
            echo "<table>";
            echo "<tr>";
            echo "<th>Name</th>";
            echo "<th>Age</th>";
            echo "<th>Sex</th>";
            echo "<th>WPM</th>";
            echo "</tr>";
            foreach ($results as $row) {
                echo "<tr>";
                echo "<td>" . $row['name'] . "</td>";
                echo "<td>" . $row['age'] . "</td>";
                echo "<td>" . $row['sex'] . "</td>";
                echo "<td>" . $row['wpm'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
        }else{
    
            echo "no results available";
        }
    }
    catch (PDOException $e) {
        echo "Error!: " . $e->getMessage() . "<br/>";
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?