0

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?

6
  • 1
    What is your expected output? Commented Feb 23, 2015 at 13:43
  • @bono $dates[0] should output 1424995200, $dates[1] should output 1424822400, and $dates[2] should output 1424908800 Commented Feb 23, 2015 at 13:45
  • 1
    Just don't do that. mysql is deprecated as you can see in the red box. You'll have to switch to PDO or mysqli. Commented Feb 23, 2015 at 14:02
  • After the while loop, add `echo '<pre>' . print_r($dates);echo '</pre>'; and add the results to this post. Commented Feb 23, 2015 at 14:23
  • PHP is removing mysql extension at the end of the year start learning mysqli or PDO Commented Feb 23, 2015 at 14:29

3 Answers 3

2

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>";
        };
    };

 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

$dates[] = $row; is the answer
1
$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/>';
}

1 Comment

@Yash was this answer useful to you? If so please mark it as the answer. If not tell us how to further assist in solving your problem.
0

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.

3 Comments

You don't need to apologize. If you think you have a good answer, post it. If you change your mind about that, delete it.
I just feeling if the stactoverflow now is changed into 'mad' people if we post a wrong answer or question.
Most people are just trying to keep the answers section tidy by removing answers that are confusing, misleading or incomplete. Unfortunately some do not have a whole lot of diplomacy and come across as quite abrasive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.