我有很多脚本需要转换为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_中那样在脚本顶部包含连接是一个好主意吗?

谢谢

最佳答案

这两者在PDO中有什么区别:
  
  $ station_stmt = $ dbh-> query($ station_query); $ station_stmt =
  self :: $ tdb-> prepare($ 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
  ->获取(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
}

关于php - 如何将PHP脚本正确转换为PDO?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26631862/

10-16 14:41