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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。