I’ve been struggling with some migration work and I was wondering if someone might be able to shed some light on why my code isn’t working.
My site connects to a MySQL db and pulls data into an array and then displays it in another file elsewhere on the site. My code to connect to the database is:
// Connect to Database
if(!session_id ()){
session_start();
}
function dbConnect() {
/* conecting to the mySQL database */
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$db = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
return $db;
}
// Disconnect from Database
function dbClose($dbConnection) {
mysqli_close($dbConnection); // Close connection to database.
}
// Get Prayer times
$prayerTimes = getPrayerTimes();
function getPrayerTimes() {
// Return the details for this month
$db = dbConnect();// Connect to database
if($conn !== false ) {
// Fetch current date
$thisday = date("Ymd");
$result = $db->query("SELECT GregDate, Fajr, FajrJam, Sunrise, Dhuhr, DhuhrJam, Asr, AsrJam, Maghrib, Ishaa, IshaaJam FROM PrayerTimes WHERE GregDate = '$thisday'");
while($row=$result->fetch_assoc()){
$prayerTimes[] = $row;
}
return $prayerTimes;
echo mysqli_error($db);
dbClose($db);
} else {
die("Connection failed: " . mysqli_connect_error());
}
}
My page code is:
// Return the details for this month
getPrayerTimes();
echo $prayerTimes[‘FajrJam’];
I do not get an output from this. I have a strong suspicion that this is because the output of GregDate is a string and so I’m not sure if I can compare it with $thisDay (although this used to work in PHP5 and the spec says the output of the date function is also a string). When I var_dump the output is NULL. The GregDate entries in the db table are in the format of Ymd i.e. 2019-01-01.
Any help would be much appreciated.