简体   繁体   English

在php中显示SQL查询结果

[英]Display SQL query results in php

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 限制从 min 到 max 的随机数

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']; 
}

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.另外我强烈建议不要使用 mysql_* ,因为它已被弃用。 Instead use the mysqli or PDO extension.而是使用mysqliPDO扩展。 You can read more about that here .您可以在此处阅读更多相关信息。

You cannot directly see the query result using mysql_query().使用 mysql_query() 无法直接看到查询结果。 It just fires the query in mysql, nothing else.它只是在 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.这会给你结果。

Strangely, none of the while loops in the other answers worked for me, they did not throw an error, but their echo was empty.奇怪的是,其他答案中的while循环都不适合我,它们没有抛出错误,但它们的回显是空的。 It worked when using foreach instead:它在使用foreach时起作用:

foreach($result as $row) {
    //echo $row['column_name']; // Print a single column data
    echo print_r($row);       // Print the entire row data
}

Idea from: PDO looping through and printing fetchAll想法来自: PDO 循环并打印 fetchAll

I had to change我不得不改变

$result = mysql_query($sql);

to

$result = $conn->query($sql);

and then use the foreach (thanks to questionto42)然后使用 foreach(感谢 questionto42)

$result = $conn->query($sql);
foreach($result as $row) {
    echo $row['column_name'];
}

Don't know if it matters but, I'm on a hosting company with: Apache Version 2.4.51 PHP Version 7.3.33 MySQL Version 5.6.41-84.1不知道这是否重要,但我在一家托管公司工作:Apache 版本 2.4.51 PHP 版本 7.3.33 MySQL 版本 5.6.41-84.1

$query ="
  SELECT 
   *
  FROM
    users
";
die($query);

In this case your query print on the browser copy this query and run on SQL Server.在这种情况下,您在浏览器上打印的查询复制此查询并在 SQL Server 上运行。

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

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