如何从 php 进行的 SQL 查询中获取值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2447701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get values from SQL query made by php?
提问by Rella
So I made a query like this
所以我做了一个这样的查询
global $connection;
$query = "SELECT *
FROM streams ";
$streams_set = mysql_query($query, $connection);
confirm_query($streams_set);
in my DB there are filds
在我的数据库中有 filds
ID, UID, SID, TIME (all INT type exept time)
ID、UID、SID、TIME(所有 INT 类型的时间除外)
So I am triing to print query relult into form
所以我试图将查询结果打印成表单
<form>
<select class="multiselect" multiple="multiple" name="SIDs">
<?php
global $connection;
$query = "SELECT *
FROM streams ";
$streams_set = mysql_query($query, $connection);
confirm_query($streams_set);
$streams_count = mysql_num_rows($streams_set);
for ($count=1; $count <= $streams_count; $count++) {
echo "<option value=\"{$count}\"";
echo ">{$count}</option>";
}
?>
</select>
<br/>
<input type="submit" value="Submit Form"/>
</form>
How to print out as "option" "values" SID's from my sql query?
如何从我的 sql 查询中打印出“选项”“值”SID?
回答by fire
while ($row = mysql_fetch_array($streams_set)) {
echo '<option value="'.$row['SID'].'">'.$row['SID'].'</option>';
}
回答by Alex
<form>
<select class="multiselect" multiple="multiple" name="SIDs">
<?php
global $connection;
$query = "SELECT *
FROM streams ";
$queryResult = mysql_query($query, $connection);
while($row = mysql_fetch_assoc($queryResult)) {
echo '<option value="'. $row["id"] .'">'. $row["title"] .'</option>';
}
?>
</select>
<br/>
<input type="submit" value="Submit Form"/>
</form>
You just have to replace the idand titleindex with your appropriate fields.
您只需要用适当的字段替换id和title索引。
回答by DrLazer
or mysql_fetch_object()
或 mysql_fetch_object()
回答by Seaux
Ole Jak, take a look at PHP's mysql_fetch_array http://php.net/manual/en/function.mysql-fetch-array.php, that's what you'll want to do in a while loop :-)
Ole Jak,看看 PHP 的 mysql_fetch_array http://php.net/manual/en/function.mysql-fetch-array.php,这就是你想要在 while 循环中做的事情 :-)

