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];
- and here is what i get
这就是我得到的
In general I need random number limited from min to max by the table id
一般来说,我需要通过表id限制从min到max的随机数
3 个解决方案
#1
5
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'];
}
#2
5
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 mysqli
or PDO
extension. You can read more about that here.
此外,我强烈建议不要使用mysql_ *,因为它已被弃用。而是使用mysqli或PDO扩展。你可以在这里阅读更多相关信息。
#3
2
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;
这会给你结果;
#1
5
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'];
}
#2
5
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 mysqli
or PDO
extension. You can read more about that here.
此外,我强烈建议不要使用mysql_ *,因为它已被弃用。而是使用mysqli或PDO扩展。你可以在这里阅读更多相关信息。
#3
2
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;
这会给你结果;