PHP使用while(mysql\u fetch\u数组)向数组添加值?

PHP使用while(mysql\u fetch\u数组)向数组添加值?,php,mysql,arrays,Php,Mysql,Arrays,我试图在从mysql查询获取数据后向数组添加值,这显然涉及while$x=mysql\u fetch\u array$MysqlQuery{},如下所示: $CheckTime = mysql_query("SELECT * FROM cp11641_timetable.booking"); $dates = array(); while ($date = mysql_fetch_array($CheckTime)) { $DateInt =

我试图在从mysql查询获取数据后向数组添加值,这显然涉及while$x=mysql\u fetch\u array$MysqlQuery{},如下所示:

      $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] . " ";
      }
但是,当我回显$dates[x]时,它将在数组的x位置显示该值,但它将按x+1倍显示该值,即$dates[0]将显示一次'a',dates[1]将显示两次'b',而$dates[2]将显示三次'c'

我该如何解决这个问题?问题出在哪里?

我建议您使用mysql\u fetch\u assoc,然后使用html/css样式水平或垂直显示日期。 对不起,如果我的答案是错误的选择

$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.
}
您的问题是使用mysql\u fetch\u数组。然后尝试使用$date['date']。如果要将列名用作$date数组中的索引。您需要使用mysql\u fetch\u assoc

另一方面,如注释中所述,使用mysqli_*扩展或PDO。在这个答案中,我使用了mysqli_*

请注意mysqli_查询函数中的$mysql_连接。

如果您使用下面的代码,它很可能会按预期工作。 仍然强烈建议切换到mysqli_*


这是可行的,请确保在步骤1中输入正确的凭据以连接到数据库。其他一切都是错误的

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

 ?>

您的预期输出是什么?@bono$dates[0]应该输出1424995200,$dates[1]应该输出1424822400,$dates[2]应该输出1424908800。不要这样做。mysql尽可能地被弃用。您必须切换到PDO或mysqli;并将结果添加到这篇文章中。PHP将在年底删除mysql扩展。开始学习mysqli或PDO您无需道歉。如果你认为你有一个好的答案,把它贴出来。如果你改变了主意,请删除它。我只是觉得如果我们发布了错误的答案或问题,stactoverflow现在会变成“疯子”。大多数人只是试图通过删除混淆、误导或不完整的答案来保持答案部分的整洁。不幸的是,有些人没有太多的外交手段,给人的印象是相当粗糙的。@Yash这个答案对你有用吗?如果是,请将其标记为答案。如果没有,请告诉我们如何进一步协助解决您的问题。
<?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>";
        };
    };

 ?>