2015-02-23 87 views
0

我試圖從一個MySQL查詢獲取數據後,值添加到一個數組,這顯然涉及到,而($ X = mysql_fetch_array($ MysqlQuery)){},如下圖所示:PHP使用while(mysql_fetch_array)向數組添加值?

 $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] . " "; 
     } 

然而,當我回顯$日期[x],它會顯示在數組的x位置的值,但它會顯示它(x + 1)次(即$日期[0]將顯示'a'一次,$日期[1]將顯示'b'兩次,$日期[2]將顯示'c'三次)

如何解決此問題?是什麼導致了這個問題?

+1

您的預期產出是多少? – Bono 2015-02-23 13:43:53

+0

@bono'$ dates [0]'應該輸出1424995200,'$ dates [1]'應該輸出1424822400,'$ dates [2]'應該輸出1424908800 – Yash 2015-02-23 13:45:38

+1

只是不這樣做。 'mysql'被**棄用**,因爲你可以[***在紅框***中看到](http://www.php.net/mysql_connect)。你必須切換到'PDO'或'mysqli'。 – 2015-02-23 14:02:33

回答

0

我建議您使用mysql_fetch_assoc(),然後以html/css風格水平或垂直顯示日期。 對不起,如果我的回答不好選擇。

+0

你不需要道歉。如果您認爲自己有一個很好的答案,請發佈。如果您改變了想法,請將其刪除。 – tadman 2015-02-24 17:34:49

+0

我只是覺得,如果我們發佈錯誤的答案或問題,現在的stctoverflow會變成「瘋狂」的人。 – 2015-02-25 03:04:59

+0

大多數人只是試圖通過消除令人困惑,誤導或不完整的答案來保持答案部分整潔。不幸的是,有些人沒有很多的外交手段,而且磨損很大。 – tadman 2015-02-25 16:10:46

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()

在不同的筆記上,如註釋中所述,使用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/>'; 
} 
+0

@Yash這個回答對你有用嗎?如果是這樣,請將其標記爲答案。如果不告訴我們如何進一步幫助解決你的問題。 – Rimble 2015-04-10 09:45:00

1

這個作品時,請務必把你的正確的憑據連接到數據庫的步驟#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>"; 
     }; 
    }; 

?>