Mapping SQL query results in php

I go to diplay results in php from sql database. MySQL instruction is correct and does what I want in phpMyAdmin, but for some reason my code breaks on the web page

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 a random number limited from min to max by the table identifier

+6
source share
3 answers

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

You need to do a while loop to get the result from the SQL query, for example:

 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 highly recommend not using mysql_ * as it is deprecated. Use the mysqli or PDO extension instead. You can learn more about this here .

+5
source

You cannot directly see the result of the query using mysql_query, it only calls the query in mysql.

To get the result you need to add lil things to the 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 the result;

+2
source

Source: https://habr.com/ru/post/959081/


All Articles