What's wrong with this PHP code dealing with the database
Question by Rogers
Ok , so I am trying to dynamically insert elements into two arrays , which are $prov_date
and $decom_date
. The thing is that when I test the elements that are entered for both arrays .. $decom_date
has all the 2 dates pushed into the array when the status is decommissioned ,which is correct but $prov_date
only has one element pushed into the array and it should have also 2 dates as shown in the database table, but only one date is there .. what could be wrong with my code below for the provisioned status ?
$prov_sql2 = '
SELECT
date_format(StatusChangeTimestamp,"%d-%m-%Y %H:%m") as StatusChangeTimestamp,
date_format(StatusChangeTimestamp,"%e/%c/%Y %H:%m") as display_StatusChangeTimestamp,
month(StatusChangeTimestamp) as prov_month,
year(StatusChangeTimestamp) as prov_year,
date_format(StatusChangeTimestamp,"%d-%m-%Y %H:%m")as display_date,
Status
FROM
STxStatusChangeHistory
WHERE
ESN = "'.$row2["ESN"].'"';
$prov_date = array();
$decom_date = array();
$prov_result2 = mysql_query($prov_sql2);
while ($prov_row2 = mysql_fetch_array($prov_result2, MYSQL_ASSOC)) {
if ($prov_row2['Status'] == "Provisioned") {
array_push($prov_date, $prov_row2["display_date"]);
}
if ($prov_row2['Status'] == "Decommissioned") {
array_push($decom_date, $prov_row2["display_date"]);
}
}
@Starx this is what is returned from the query
array(6) { ["StatusChangeTimestamp"]=> string(16) "16-10-2010 13:10" ["display_StatusChangeTimestamp"]=> string(16) "16/10/2010 13:10" ["prov_month"]=> string(2) "10" ["prov_year"]=> string(4) "2010" ["display_date"]=> string(16) "16-10-2010 13:10" ["Status"]=> string(11) "Provisioned" } array(6) { ["StatusChangeTimestamp"]=> string(16) "18-10-2010 10:10" ["display_StatusChangeTimestamp"]=> string(16) "18/10/2010 10:10" ["prov_month"]=> string(2) "10" ["prov_year"]=> string(4) "2010" ["display_date"]=> string(16) "18-10-2010 10:10" ["Status"]=> string(14) "Decommissioned" } array(6) { ["StatusChangeTimestamp"]=> string(16) "12-10-2010 13:10" ["display_StatusChangeTimestamp"]=> string(16) "12/10/2010 13:10" ["prov_month"]=> string(2) "10" ["prov_year"]=> string(4) "2010" ["display_date"]=> string(16) "12-10-2010 13:10" ["Status"]=> string(12) "Provisioned " } array(6) { ["StatusChangeTimestamp"]=> string(16) "14-10-2010 13:10" ["display_StatusChangeTimestamp"]=> string(16) "14/10/2010 13:10" ["prov_month"]=> string(2) "10" ["prov_year"]=> string(4) "2010" ["display_date"]=> string(16) "14-10-2010 13:10" ["Status"]=> string(14) "Decommissioned" } array(1) { [0]=> string(16) "16-10-2010 13:10" }
Answer by Dave Hale
You have a space after the provisioned in your db table for the 2nd entry.
Use trim
maybe to ensure no whitespace carried over from the db.
if (trim($prov_row2['Status']) == "Provisioned") {
Answer by Starx
How about you change your implementation. Is basically does the same thing.
$prov_date[] = $prov_row2["display_date"];