【问题标题】:PHP Adding values to an array using while(mysql_fetch_array)?PHP使用while(mysql_fetch_array)向数组添加值?
【发布时间】:2015-02-25 01:46:26
【问题描述】:

我试图在从 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' 三次)

我该如何解决这个问题?是什么导致了问题?

【问题讨论】:

  • 你的预期输出是什么?
  • @bono $dates[0] 应该输出 1424995200,$dates[1] 应该输出 1424822400,$dates[2] 应该输出 1424908800
  • 别那样做。 mysql弃用,您可以使用 see in the red box。你必须切换到PDOmysqli
  • 在 while 循环之后,添加 `echo '
    ' 。 print_r($dates);echo '
    ';并将结果添加到这篇文章中。
  • PHP 将在年底删除mysql 扩展名开始学习mysqliPDO

标签: php mysql arrays


【解决方案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.
}

您的问题是您使用mysql_fetch_array。但是然后尝试使用$date['Date']。如果要将列名用作$date 数组中的索引。您需要使用mysql_fetch_assoc()

另外,如 cmets 中所述,使用 mysqli_* 扩展名或 PDO。在这个答案中,我使用了mysqli_*

请注意 mysqli_query 函数中的$mysql_connectionMySqli Query Doc

如果您使用下面的代码,它很可能会按预期工作。 还是强烈建议切换到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/>';
}

【讨论】:

  • @Yash 这个答案对你有用吗?如果是这样,请将其标记为答案。如果没有,请告诉我们如何进一步协助解决您的问题。
【解决方案2】:

这可行,请确保输入正确的凭据以在第 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>";
        };
    };

 ?>

【讨论】:

    【解决方案3】:

    我建议您使用mysql_fetch_assoc(),然后以html/css 样式水平或垂直显示日期。 对不起,如果我的回答不好,请选择。

    【讨论】:

    • 你不需要道歉。如果您认为您有一个好的答案,请发布它。如果您改变主意,请删除它。
    • 我只是觉得如果我们发布错误的答案或问题,现在的 stactoverflow 是否会变成“疯狂”的人。
    • 大多数人只是想通过删除令人困惑、误导或不完整的答案来保持答案部分的整洁。不幸的是,有些人没有太多的外交技巧,而且给人的印象很粗暴。
    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 2015-08-05
    • 2012-08-19
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多