gpt4 book ai didi

php - 查询 mysql php

转载 作者:行者123 更新时间:2023-11-29 00:39:26 24 4
gpt4 key购买 nike

我正在尝试从 entradas where data ='some date'; 中选择 hora

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 |+------------+----------+

I can do it in mysql:

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

but when I try it in 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 表格,如下所示:

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

最佳答案

您应该考虑使用 PDO ( tutorial ) 而不是过时的 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>';

关于php - 查询 mysql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12893141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com