简体   繁体   English

PHP:从数据库中选择

[英]PHP : Select from Database

thank you for reading. 感谢您的阅读。 I'm having a little problem with the next sentence: 我的下一个句子有点问题:

$nats = mysql_query("SELECT id,name FROM reportSatus WHERE reportId = ". $_POST['id']);
$rnat = mysql_fetch_array($nats);

With print_r($rnat) : 使用print_r($ rnat):

Array(
[0] => 1
[id] => 1
[1] => Poca contaminacion
[name] => Poca contaminacion
[2] => 1
[reportId] => 1)

But in the database with the same sentence is: 但是在数据库中用同样的句子是:

id            name
1       Poca contaminacion
2       Mucha contaminacion

Any idea what can it be? 知道会是什么吗? Thank you in advance ~ 预先谢谢你〜

try : 尝试:

echo '<table><tr><th>id</th><th>name</th></tr>';
while ($row = mysql_fetch_assoc($nats)) {
    echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td></tr>";
}
echo '</table>';

From documentation 从文档

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. 返回与获取的行相对应的字符串数组;如果没有更多行,则返回FALSE。 The type of returned array depends on how result_type is defined. 返回数组的类型取决于result_type的定义方式。 By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. 通过使用MYSQL_BOTH(默认),您将获得一个具有关联索引和数字索引的数组。 Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works). 使用MYSQL_ASSOC,您只会获得关联索引(如mysql_fetch_assoc()起作用);使用MYSQL_NUM,您只会获得数字索引(如mysql_fetch_row()起作用)。

That's actually the way it works, if you need more rows to be returned you will need to loop throw the records 这实际上就是它的工作方式,如果您需要返回更多行,则需要循环抛出记录

while($rnat = mysql_fetch_array($nats))
{
    //print here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM