1

我正在努力select hora from entradas where data ='some date';

桌子entradas

+------------+----------+
| 数据 | 霍拉 |
+------------+----------+
| 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中做到这一点:

+----------+
| 霍拉 |
+----------+
| 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 行变成 1 行并给我结果09:29:1609:43:0509:43:14

如何将结果集输出为 HTML 中的表格,如下所示:

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

5 回答 5

1

您应该考虑使用PDO教程)而不是过时的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>';
于 2012-10-15T10:07:26.743 回答
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++;
}
于 2012-10-15T10:23:30.587 回答
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++;
}
于 2012-10-15T10:32:11.373 回答
0
while($row = mysql_fetch_array($consulta_data))
    {
        $response[] = $row[0];
    }

然后 $response 应该包含你想要的

于 2012-10-15T09:55:00.973 回答
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>
于 2012-10-15T10:42:50.367 回答