在php中显示SQL查询结果 [英] Display SQL query results in php

查看:368
本文介绍了在php中显示SQL查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从sql数据库中显示php中的结果 MySQL语句是正确的,并且可以在phpMyAdmin中执行我想要的操作,但是由于某些原因,我的代码在网页中中断了

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

这是代码

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
    • 通常,我需要由表ID限制从最小到最大的随机数

      In general I need random number limited from min to max by the table id

      推荐答案

      您需要从查询获得的结果集中的每一行中获取数据.您可以为此使用mysql_fetch_array().

      You need to fetch the data from each row of the resultset obtained from the query. You can use mysql_fetch_array() for this.

      // 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
      }
      

      将代码更改为此:

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

      这篇关于在php中显示SQL查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆