PHP使用while(mysql_fetch_array)向数组添加值


PHP Adding values to an array using while(mysql_fetch_array)?

我试图在从mysql查询中获取数据后向数组添加值,这显然需要一段时间($x=mysql_fetch_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")

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

$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_fetch_array。但是尝试使用$date['Date']。如果要将列名用作$date数组中的索引。您需要使用mysql_fetch_assoc()

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

请注意mysqli_query函数中的$mysql_connection。MySqli查询文档

如果您使用下面的代码,它很可能会按预期工作。仍然强烈建议切换到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中输入正确的凭据以连接到数据库。其他一切都是错误的。

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

我建议您使用mysql_fetch_assoc(),然后使用html/css样式水平或垂直显示日期。如果我的答案是错误的选择,很抱歉。