php 在php中显示SQL查询结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20300582/
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
Display SQL query results in php
提问by user3052127
I'm tring to diplay results in php from sql database MySQL statement is correct and does what i want in phpMyAdmin but for some reason my code breaks in the webpage
我正在尝试从 sql 数据库中显示 php 中的结果 MySQL 语句是正确的,并且在 phpMyAdmin 中执行我想要的操作,但是由于某种原因,我的代码在网页中中断
here is the code
这是代码
require_once('db.php');
$sql="SELECT * FROM modul1open WHERE idM1O>=(SELECT FLOOR( MAX( idM1O ) * RAND( ) ) FROM modul1open)
ORDER BY idM1O LIMIT 1"
$result = mysql_query($sql);
echo [$result];
In general I need random number limited from min to max by the table id
一般来说,我需要由表 id 限制从最小到最大的随机数
采纳答案by Aditya Vikas Devarapalli
You need to fetch the data from each row of the resultset obtained from the query. You can use mysql_fetch_array()for this.
您需要从查询获得的结果集的每一行中获取数据。您可以mysql_fetch_array()为此使用。
// Process all rows
while($row = mysql_fetch_array($result)) {
echo $row['column_name']; // Print a single column data
echo print_r($row); // Print the entire row data
}
Change your code to this :
将您的代码更改为:
require_once('db.php');
$sql="SELECT * FROM modul1open WHERE idM1O>=(SELECT FLOOR( MAX( idM1O ) * RAND( ) ) FROM modul1open)
ORDER BY idM1O LIMIT 1"
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row['fieldname'];
}
回答by display-name-is-missing
You need to do a while loop to get the result from the SQL query, like this:
您需要执行 while 循环才能从 SQL 查询中获取结果,如下所示:
require_once('db.php');
$sql="SELECT * FROM modul1open WHERE idM1O>=(SELECT FLOOR( MAX( idM1O ) * RAND( ) )
FROM modul1open) ORDER BY idM1O LIMIT 1";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// If you want to display all results from the query at once:
print_r($row);
// If you want to display the results one by one
echo $row['column1'];
echo $row['column2']; // etc..
}
Also I would strongly recommend not using mysql_* since it's deprecated. Instead use the mysqlior PDOextension. You can read more about that here.
此外,我强烈建议不要使用 mysql_*,因为它已被弃用。而是使用mysqli或PDO扩展名。您可以在此处阅读更多相关信息。
回答by Neeraj Kumar
You cannot directly see the query result using mysql_query its only fires the query in mysql nothing else.
您不能使用 mysql_query 直接查看查询结果,它只会触发 mysql 中的查询,没有别的。
For getting the result you have to add a lil things in your script like
为了得到结果,你必须在你的脚本中添加一个小东西,比如
require_once('db.php');
$sql="SELECT * FROM modul1open WHERE idM1O>=(SELECT FLOOR( MAX( idM1O ) * RAND( ) ) FROM modul1open) ORDER BY idM1O LIMIT 1";
$result = mysql_query($sql);
//echo [$result];
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row);
}
This will give you result;
这会给你结果;

