我有很多需要转换为PDO的脚本。我的脚本在mysqli_中,我一直在网上阅读不同的方式来做同样的事情。有人能解释一下这些差异吗?如何正确地将PHP脚本转换为PDO?
再次,对不起,如果我要求简单的东西,但在线文件混淆了我。
在此先感谢。
我原来的PHP脚本mysqli_:
<?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());
}
对于mysqli_ while循环PDO我用这个:
while($row = $station_stmt -> fetch(PDO::FETCH_ASSOC))
有没有这样做它的另一种方式?
最后,在脚本的顶部是否包含连接,如mysqli_中的一个好主意?
谢谢
我不能回答你的questions.But我刚去海槽PDO此YouTube教程。很好。如果您对https://www.youtube.com/playlist感兴趣?list = PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc – EL3PHANTEN 2014-10-29 13:51:26
反问题:你真的把'mysqli_'与PDO混合了吗? – 2014-10-29 13:54:14
@ Fred-ii-不,我不是,我在纯mysqli_中发布了我的原始脚本,并在其下面使用了我在PDO中使用的脚本。我得到一些错误 – mario 2014-10-29 13:55:56