如何正确地将PHP脚本转换为PDO?

时间:2014-10-29 13:38:40

标签: php mysql pdo mysqli

我有很多脚本需要转换为PDO。我的脚本是mysqli_,我一直在网上阅读不同的方法来做同样的事情。有人可以解释一下这些差异吗?

再次,抱歉,如果我要求简单的东西,但网上的文件让我感到困惑。

提前致谢。

我在mysqli _中的原始PHP脚本:

    <?php
include 'db_conn_pdo.php';

//preparing query


$desk_query = "SELECT 
                                coordinate_id, 
                                x_coord, 
                                y_coord, 
                                section_name, 
                                station_number, 
                                ver_hor
                            FROM coordinates";


$station_query = "SELECT
                                    coordinate_id,
                                section_name,
                                station_number,
                                x_coord,
                                y_coord,
                                username,
                                hostname
                                FROM
                                sandbox_maesc4.coordinates c
                                INNER JOIN
                                softphone_materials_updater.workstations w
                                ON w.pod = c.station_number
                                INNER JOIN
                                sandbox_maesc4.workstation_userlogged wsu
                                ON w.ws = wsu.hostname
                                WHERE wsu.lastupdate >= CURRENT_TIMESTAMP - INTERVAL 10 MINUTEs";       
/**************************/

$desk_stmt    = $dbh->query($desk_query);

while($row  = $desk_stmt -> fetch(PDO::FETCH_ASSOC)){   
                $id       = $row['coordinate_id'];
                $x_pos    = $row['x_coord'];
                $y_pos    = $row['y_coord'];
                $sec_name = $row['section_name'];
                $sta_num  = $row['station_number'];
                $position = $row['ver_hor'];

                $class = 'desk_box_ver';
                if($position == 'horizontal'){
                    $class = 'desk_box_hor';
                }
                    echo '<div class="' . $class . '" data-rel="' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos.'px;">' . $sta_num . '</div>' . "\n";
            }

$station_stmt = $dbh->query($station_query);

while($row  = $station_stmt -> fetch(PDO::FETCH_ASSOC)){
$id       = $row['coordinate_id'];
    $sec_name = $row['section_name'];
    $sta_num  = $row['station_number'];
    $x_pos    = $row['x_coord'];
    $y_pos    = $row['y_coord'];

echo '<div class="station_info_" id="station_info_' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos . 'px;"><p class="numb">Section:' . $sec_name . '<br>Station:' . $sta_num . '<br></p></div>' . "\n";
}//end while
?>

PDO中这两者有什么区别:

$station_stmt = $dbh->query($station_query);
//and
$station_stmt = self::$tdb->prepare($station_query);

如何在mysqli_中使用这两行来检查我的查询在PDO中是否合适

$station_result = mysqli_query($conn,$station_query);

if($station_result === false) {
    die(mysqli_error()); 
}

对于PDO中的mysqli_ while循环我使用了这个:

while($row  = $station_stmt -> fetch(PDO::FETCH_ASSOC))

还有另一种方法吗?

最后,包括脚本顶部的连接是一个好主意,如mysqli _?

谢谢

1 个答案:

答案 0 :(得分:1)

  

PDO中这两者有什么区别:

     

$ station_stmt = $ dbh-&gt; query($ station_query); $ station_stmt =   自:: $ tdb-&GT;制备($ station_query);

第一个是直接查询,未准备好(例如:SELECT * FROM station

第二个正在准备中。(例如:SELECT * FROM desk WHERE id = ?

仅在查询没有参数时才使用query()

如果要将值绑定到查询

,请使用prepare()
  

如何在mysqli_中使用这两行来检查我的查询是否正确   擅长PDO

     

$ station_result = mysqli_query($ conn,$ station_query);   if($ station_result === false){die(mysqli_error()); }

同样的事情,检查query()是否返回false

$station_stmt = $dbh->query($station_query);
if (!$station_stmt) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
    die();
}
  

对于PDO中的mysqli_ while循环我使用了这个:while($ row = $ station_stmt    - &GT;取(PDO :: FETCH_ASSOC))

PDO提供fetchAll(),您可以使用它一次获取所有行:

$row = $station_stmt -> fetchAll(PDO::FETCH_ASSOC);

<小时/> MINUTE不会s

CURRENT_TIMESTAMP -  INTERVAL 10 MINUTE

尝试这种方法:

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if(!$dbh){
        echo "\nPDO::errorInfo():\n";
        print_r($dbh->errorInfo());
        die();
    }

    get_desk_coordinates($dbh);
    get_station_coordinates($dbh);


} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}


/**
 * @param $dbh
 */
function get_desk_coordinates($dbh)
{
    $desk_query = "SELECT
                    coordinate_id,
                    x_coord,
                    y_coord,
                    section_name,
                    station_number,
                    ver_hor
               FROM coordinates";

    $desk_stmt = $dbh->query($desk_query);

    while ($row = $desk_stmt->fetch(PDO::FETCH_ASSOC)) {
        $id = $row['coordinate_id'];
        $x_pos = $row['x_coord'];
        $y_pos = $row['y_coord'];
        $sec_name = $row['section_name'];
        $sta_num = $row['station_number'];
        $position = $row['ver_hor'];

        $class = 'desk_box_ver';
        if ($position == 'horizontal') {
            $class = 'desk_box_hor';
        }
        echo '<div class="' . $class . '" data-rel="' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos . 'px;">' . $sta_num . '</div>' . "\n";
    }
}



/**
 * @param $dbh
 */
function get_station_coordinates($dbh)
{
    $station_query = "  SELECT
                    coordinate_id,
                    section_name,
                    station_number,
                    x_coord,
                    y_coord,
                    username,
                    hostname
                    FROM sandbox_maesc4.coordinates c
                    INNER JOIN
                    softphone_materials_updater.workstations w
                    ON w.pod = c.station_number
                    INNER JOIN
                    sandbox_maesc4.workstation_userlogged wsu
                    ON w.ws = wsu.hostname
                    WHERE wsu.lastupdate >= CURRENT_TIMESTAMP -  INTERVAL 10 MINUTE";

    $station_stmt = $dbh->query($station_query);

    while ($row = $station_stmt->fetch(PDO::FETCH_ASSOC)) {
        $id = $row['coordinate_id'];
        $sec_name = $row['section_name'];
        $sta_num = $row['station_number'];
        $x_pos = $row['x_coord'];
        $y_pos = $row['y_coord'];

        echo '<div class="station_info_" id="station_info_' . $id . '" style="left:' . $x_pos . 'px;top:' . $y_pos . 'px;"><p class="numb">Section:' . $sec_name . '<br>Station:' . $sta_num . '<br></p></div>' . "\n";
    }
    //end while
}