Reputation: 1
I'm trying to add values to an array after getting data from a mysql query, this obviously involved a while ($x = mysql_fetch_array($MysqlQuery)) {} as seen below:
$CheckTime = mysql_query("SELECT * FROM cp11641_timetable.booking");
$dates = array();
while ($date = mysql_fetch_array($CheckTime)) {
$DateInt = strtotime($date['Date']);
//echo $DateInt . " ";
$dates[] = $DateInt;
echo $dates[1] . " ";
}
However when I echo $dates[x], it'll display the value in the x position of the array, but it'll show it by (x+1) times (i.e. $dates[0] will show 'a' once, $dates[1] will show 'b' twice, and $dates[2] will show 'c' thrice)
How do I fix this? What's causing the problem?
Upvotes: 0
Views: 1403
Reputation: 56
This works, make sure to put in your correct credentials to connecting to your database on step #1. everything else false in place.
<?php
/* ==============================================
This is the new way of connecting to database
using mysqli
================================================*/
// Step #1 create credentiasl for database connection
$host = ""; //type your host ex. localhost between the quotes
$user = ""; //your username between the quotes
$pass = ""; //your password between the quotes
$db = ""; //your database you are connecting to between the quotes
// step #2 create connection to database
$conn = new mysqli($host, $user, $pass, $db);
//step #3 check and see if connection is working and error free
if ($conn->error) {
die("Could not connect to the database");
} else{
// create array dates
$dates = array();
// create select statement
$CheckTime = ("SELECT * FROM cp11641_timetable.booking");
// query the the database using the connection
$sql_CheckTime = $conn->query($CheckTime);
// if rows available in table add them to array dates
while ($row = mysqli_fetch_assoc($sql_CheckTime)) {
$dates[] = $row;
}
//optional uncomment bottom line to check if dates array has data will display as array on webpage
// var_dump($dates);
// loop through array
foreach ($dates as $date){
// echo out data you want to display. 'Date' = column name
echo strtotime($date['Date']) . "<br>";
};
};
?>
Upvotes: 2
Reputation: 893
$CheckTime = mysqli_query($mysql_connection, "SELECT * FROM cp11641_timetable.booking");
$dates = array();
while ($date = mysqli_fetch_assoc()($CheckTime)){ // Use mysqli_* for queries.
$DateInt = strtotime($date['Date']); // This will show an UNIX timestamp
$dates[] = $DateInt; // Fills the array with the timestamp.
}
Your problem is that you use mysql_fetch_array
. But then try to use $date['Date']
. If you want to use the column names as indices in the $date
array. You need to use mysql_fetch_assoc()
.
On a different note and as mentioned in the comments use the mysqli_*
extension or PDO. In this answer I've used mysqli_*
Please note the $mysql_connection
in the mysqli_query function.
MySqli Query Doc
Most likely if you use the code below it should work as intended.
Still strongly advise to switch to mysqli_*
$CheckTime = mysql_query("SELECT Date FROM cp11641_timetable.booking");
$dates = array();
while ($date = mysql_fetch_array($CheckTime)){
$DateInt = strtotime($date[0]);
$dates[] = $DateInt;
}
foreach($dates as $timestamp){
echo $timestamp . '<br/>';
}
Upvotes: 1
Reputation: 59
I recommend you to using mysql_fetch_assoc()
and then display the dates horizontally or vertically with html/css style.
Sorry if my answer is bad choose.
Upvotes: 0