Search code examples
phpmysqlrandomnumbersgenerator

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

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


Solution

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

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