如何正确地将PHP脚本转换为PDO?
我有很多需要转换为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_中的一个好主意?
谢谢
What is the difference between these two in PDO:
$station_stmt = $dbh->query($station_query); $station_stmt = self::$tdb->prepare($station_query);
第一种是直的查询,其不备(如:SELECT * FROM station
)。
第二个正在准备(如:SELECT * FROM desk WHERE id = ?
)
仅当查询没有参数时才使用query()
当您想要绑定val时使用prepare()
UE能够查询
and how can I use these two lines in mysqli_ to check if my query is good in 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();
}
For mysqli_ while loop in PDO I used this: while($row = $station_stmt -> fetch(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
}
我不能回答你的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