Display SQL query results in php

前端 未结 3 1741
春和景丽
春和景丽 2020-12-03 14:58

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

3条回答
  •  不知归路
    2020-12-03 15:54

    You need to do a while loop to get the result from the SQL query, like 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, 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.

提交回复
热议问题