2012-10-15 99 views
1

我正在嘗試select hora from entradas where data ='some date';查詢MySQL php

entradas

 
+------------+----------+ 
| data  | hora  | 
+------------+----------+ 
| 2012-09-27 | 07:59:11 | 
| 2012-09-27 | 16:03:27 | 
| 2012-09-28 | 16:03:35 | 
| 2012-09-29 | 09:29:16 | 
| 2012-09-29 | 09:43:05 | 
| 2012-09-29 | 09:43:14 | 
| 2012-10-01 | 08:03:10 | 
+------------+----------+ 

我能做到這一點在MySQL:

 
+----------+ 
| hora  | 
+----------+ 
| 09:29:16 | 
| 09:43:05 | 
| 09:43:14 | 
+----------+ 

但是當我嘗試在PHP:

$consulta_data = mysql_query(" 
    select hora from entradas where data = '2012-09-29'; 
") or die(mysql_error()); 

while($row = mysql_fetch_array($consulta_data)) 
{ 
    echo $row[0]; 
} 

的3行轉中一個給我結果09:29:1609:43:0509:43:14

我如何輸出的結果集,作爲HTML中的表,如下:

 
row[0]=09:29:16 
row[1]=09:43:05 
row[2]=09:43:14 

回答

0
while($row = mysql_fetch_array($consulta_data)) 
    { 
     $response[] = $row[0]; 
    } 

然後$響應應該包含要

+0

你好,我嘗試它像你說的,讓這種致命的錯誤:無法使用[]在閱讀第80行的C:\ wamp \ www \ Controlo Entradas \ importar_BD.php – user1746620

+0

80行是什麼?第80行中的 – nico

+0

是這個echo $ response []; – user1746620

1

您應該考慮使用PDOtutorial)是什麼,而不是過時的mysql_

$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password'); 
$result = array(); 

$stmt = $db->prepare("SELECT hora FROM entradas WHERE data = ?"); 
$stmt->setFetchMode(PDO::FETCH_NUM); 
$stmt->execute('2012-09-29'); 
foreach($sth as $row) { 
    $result[] = $row[0]; 
} 
return $result; 

或當你想它作爲HTML,請用這個foreach循環:

echo '<table><tbody>'; 
foreach($sth as $row){ 
    echo '<tr><td>' . htmlspecialchars($row[0]) . '</td></tr>'; 
} 
echo '</tbody></table>'; 
+0

你好,再次嘗試做什麼是這樣的我有一個mysql db與f_name | l_name | card_number |日期|時間。我想打印這個像html這樣的f_name,u_name | card_number |日期| time1 | time2 |和更多的時間,如果在數據庫相同的日期多次你是否認爲我?有人可以幫助請 – user1746620

+0

嗨再次Vyktor有沒有辦法聯繫你發給你我想要做的全部?這樣我可以更好地向你解釋 – user1746620

+0

@ user1746620不,我不會再給你任何聯繫。我們不會爲您編寫代碼。我提供了完整文檔鏈接,教程鏈接。你可能會問另一個問題,但如果你懶惰做自己的研究並嘗試至少一段時間,這裏沒有人會幫助你。 – Vyktor

0

我不知道我的正確與否了你的問題,因爲它似乎很容易,通過它可能工作的方式:

echo $row[0] . '<br>'; 

你只需要分開輸出?如果我錯了,請讓我知道更改代碼。

你真的想要這種格式嗎?我的意思是與row[0]也是?

row[0]=09:29:16 

則:

$i = 0; 

while($row = mysql_fetch_array($consulta_data)) 
{ 
    echo 'row[' . $i . '] = ' . $row[0] . '<br>'; 
    $i++; 
} 
0

讓我們試試這個,

$consulta_data = mysql_query(" 
    select hora from entradas where data = '2012-09-29'; 
") or die(mysql_error()); 
$i=0; 
while($row = mysql_fetch_array($consulta_data)) 
{ 
    echo "row[0] = ".$row[0]."<br />"; 
    $i++; 
} 
0
<?php 
$result = array(); 

$consulta_data = mysql_query("select hora from entradas where data = '2012-09-29';") or die(mysql_error()); 

while($row = mysql_fetch_array($consulta_data)) { 
    $result[] = $row[0]; 
} 
?> 

<table border="1"> 
    <thead> 
     <tr> 
      <td>Hora</td> 
     </tr> 
    </thead> 
    <tbody> 
     <?php foreach($result as $hora): ?> 
      <tr><td><?php echo $hora; ?></td></tr> 
     <?php endforeach; ?> 
    </tbody> 
</table>