I am trying to convert an existing MYSQL script into PDO. My MYSQL script works but my PDO version does not.
The aim of the script is to SELECT from the TABLE schedule certain UID's where UID's that are the same are chosen by the lowest alphabetical letter of their corresponding cif_stp_indicator.
So for example I am trying to locate UID number 'Y63553'. There are two UID's with this number. One has an cif_stp_indicator of 'N' and one with a cif_stp_indicator of 'O'. I wish to chose the UID with an STP indicator of 'N'
so example data............
cif_train_uid cif_stp_indicator Other Data
Y63553 N x
Z45789 O x
T78456 O x
Y63553 O x
I wish to choose the Y63553 with an cif_stp_indicator of 'N'
The below MYSQL script achieves that
$cif_stp_indicator=null;
$cif_train_uid='Y63553';
$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' ";
$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'];
}
}
The below PDO script runs but it gives no results. I am sure its to do with the way the NULL part is configured as I can get the script to show both cif_train_uid's of Y63553 by removing the relevant parts of the script. I have had help from this forum to get this far with the PDO script and it is now just trying to understand why the NULL part of the script is not working. ie not reporting back Y63553 and N
$cif_stp_indicator=null;
$cif_train_uid='Y63553';
$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.cif_train_uid=:s1_cif_train_uid ";
$movedata=$mysql_link->prepare($sql);
$movedata->bindValue(':s1_cif_train_uid',$cif_train_uid, 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'];
}
Code for connecting
try
{
$mysql_link= new PDO("mysql:host=$servername;dbname=db;charset=utf8",$username,$password);
$mysql_link->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
$mysql_link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
echo "Connected Success";
}
catch(PDOException $e)
{
echo $e->getMessage();
}