我正在学习PDO并且已经将我的普通mysql代码转换为PDO代码。我被困在LEFT连接位代码上。我得到一个错误,说我有一个意外的'S1'(T-String)...........指的是我的SELECT行
代码如下
$cif_stp_indicator=Null;
$cif_train_uid="Y63553";
$schedule_start_date="2015-01-24";
$schedule_end_date="2015-01-24";
$movedata=$mysql_link->prepare(SELECT s1.cif_train_uid,s1.cif_stp_indicator,s1.schedule_start_date
FROM schedule s1
LEFT JOIN schedule s2
ON (s1.cif_train_uid=s2.cif_train_uid AND s1.cif_stp_indicator>s2.cif_stp_indicator)
WHERE s2.cif_stp_indicator=:s2.cif_stp_indicator AND s1.cif_train_uid=:s1.cif_train_uid AND s1.schedule_start_date<=:s1.schedule_start_date AND s1.schedule_end_date>=:s1.schedule_end_date);
$movedata->execute(array(':s2.cif_stp_indicator'=>$cif_stp_indicator,':s1.cif_train_uid'=>$cif_train_uid,':s1.schedule_start_date'=>$schedule_start_date,':schedule_end_date'=>$schedule_end_date));
foreach($movedata->fetchAll(PDO::FETCH_ASSOC) as $row) {
echo $row['cif_train_uid'];
echo $row['cif_stp_indicator'];
}
请问你能解决问题所在。
编辑 - 这是我试图代表PDO的MYSQL版本。这可以作为MYSQL
$cif_stp_indicator=null;
$cif_train_uid='Y63553';
$schedule_start_date='2015-01-24';
$schedule_end_date='2015-01-24';
$b="SELECT s1.cif_train_uid,s1.cif_stp_indicator,s1.schedule_start_date
FROM schedule s1
LEFT JOIN schedule s2
ON (s1.cif_train_uid=s2.cif_train_uid AND s1.cif_stp_indicator>s2.cif_stp_indicator)
WHERE s2.cif_stp_indicator is NULL AND s1.cif_train_uid='$cif_train_uid' AND s1.schedule_start_date<='$schedule_start_date' AND s1.schedule_end_date>='$schedule_end_date'";
$l=mysqli_query($mysql_link,$b);
if ($l) {
while($berths=mysqli_fetch_array($l,MYSQLI_ASSOC))
{
echo $berths['cif_train_uid'];
echo $berths['cif_stp_indicator'];
}
}
最终工作答案
$cif_train_uid='Y63553';
$schedule_start_date='2015-01-24';
$schedule_end_date='2015-01-24';
$sql="SELECT s1.cif_train_uid,s1.cif_stp_indicator,s1.schedule_start_date
FROM schedule s1
LEFT JOIN schedule s2
ON (s1.cif_train_uid=s2.cif_train_uid AND s1.cif_stp_indicator>s2.cif_stp_indicator)
WHERE s2.cif_stp_indicator is NULL AND s1.schedule_start_date=:s1_schedule_start_date AND s1.schedule_end_date=:s1_schedule_end_date";
$movedata=$mysql_link->prepare($sql);
$movedata->bindValue(':s1_schedule_start_date',$schedule_start_date, PDO::PARAM_STR);
$movedata->bindValue(':s1_schedule_end_date',$schedule_end_date, PDO::PARAM_STR);
$movedata->execute();
$array=$movedata->fetchAll();
for($i=0;$i<sizeof($array);$i++){
echo $array[$i]['cif_train_uid'];
echo $array[$i]['cif_stp_indicator'];
}
答案 0 :(得分:1)
你忘了引用sql字符串,你的循环也是错的,试试这个:
$sql = "
SELECT s1.cif_train_uid,s1.cif_stp_indicator,s1.schedule_start_date
FROM schedule s1
LEFT JOIN schedule s2 ON
(s1.cif_train_uid=s2.cif_train_uid AND
s1.cif_stp_indicator>s2.cif_stp_indicator)
WHERE s2.cif_stp_indicator=:s2_cif_stp_indicator AND
s1.cif_train_uid=:s1_cif_train_uid AND
s1.schedule_start_date<=:s1_schedule_start_date AND
s1.schedule_end_date>=:s1_schedule_end_date
";
$movedata=$mysql_link->prepare($sql);
$movedata->execute(array(':s2_cif_stp_indicator'=>$cif_stp_indicator,
':s1_cif_train_uid'=>$cif_train_uid,
':s1_schedule_start_date'=>$schedule_start_date,
':s1_schedule_end_date'=>$schedule_end_date
));
while($row = $movedata->fetch(PDO::FETCH_ASSOC)) {
echo $row['cif_train_uid'];
echo $row['cif_stp_indicator'];
}