dongwei1855 2015-06-27 10:36
浏览 126
已采纳

mysql_query转换为PDO或mysqli寻找示例

I have been crucified by many people for still using the old 'method' mysql_query() also I am in the process of launching my website which took me 4-months to complete and I would like to have maximum security for it.

So I thought now is probably the best time to get rid of the old habit and start using the new methods.

I have scanned over some tutorials but I have to admit it does seem rather (very) complicated compared to mysql_query()

The best way to learn is probably through example, so Im hoping someone would be kind enough to change the following example for me to a PDO and a MYSQLI statement so that I can see the difference between the two. If a brief explanation can be included it will be greatly appreciated...

Also what do you recommend is the easiest to learn? Mysqli or PDO

$sql="select * from Multiple_Picks where event_id = '$matchId' and
                  pick='$winner' and
                 abs(score-$winScore) = (select min(abs(score-$winScore)) from Multiple_Picks 
                 where pick = '$winner' );";    

    $result = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);

    while($row = mysql_fetch_array($result)){
        //if(isset($row['member_nr'])){
            $winingMember = $row['member_nr'];
            $event = $row['event_id'];
            $pick = $row['pick'];
            $score = $row['score'];
            $tournament=$row['tournament'];
            $round=$row['round'];
}

Thanks for reading

  • 写回答

1条回答 默认 最新

  • dragon87836215 2015-06-27 10:42
    关注

    this helps you

    using mysqli:

    $con = mysqli_connect("localhost",$username,$password,$dbname);
    
    $fetch=mysqli_query($con,'select * from Multiple_Picks where event_id = '$matchId' and
                      pick='$winner' and
                     abs(score-$winScore) = (select min(abs(score-$winScore)) from Multiple_Picks 
                     where pick = '$winner' );');
    while( $row=mysqli_fetch_assoc($fetch))
    {   
            $winingMember = $row['member_nr'];
                $event = $row['event_id'];
                $pick = $row['pick'];
                $score = $row['score'];
                $tournament=$row['tournament'];
                $round=$row['round'];
    }
    

    using PDO

    <?php
    /*** mysql hostname ***/
    $hostname = 'localhost';
    
    /*** mysql username ***/
    $username = 'username';
    
    /*** mysql password ***/
    $password = 'password';
    
    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
        /*** echo a message saying we have connected ***/
        echo 'Connected to database<br />';
    
        /*** The SQL SELECT statement ***/
        $sql = "select * from Multiple_Picks where event_id = '$matchId' and
                      pick='$winner' and
                     abs(score-$winScore) = (select min(abs(score-$winScore)) from Multiple_Picks 
                     where pick = '$winner' );";
    
        /*** fetch into an PDOStatement object ***/
        $stmt = $dbh->query($sql);
    
        /*** echo number of columns ***/
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
        /*** loop over the object directly ***/
        foreach($result as $key=>$val)
        {
        echo $key.' - '.$val.'<br />';
        }
    
        /*** close the database connection ***/
        $dbh = null;
    }
    catch(PDOException $e)
        {
        echo $e->getMessage();
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?