17

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

Yosi Dahari
  • 6,794
  • 5
  • 24
  • 44
user3052127
  • 173
  • 1
  • 1
  • 4
  • If the picture is your actual output it looks like you don't have PHP enabled on the server. Or at least the code is not parsed as PHP code, which can have a couple of reasons. Is the code you posted the complete content of your .php file? – Gerald Schneider Nov 30 '13 at 13:18
  • no just the fragment that doesn't work and all the rest works just fine. php breaks only there – user3052127 Nov 30 '13 at 13:35
  • also missing a ; at the end of line 2 – ScottC Dec 22 '15 at 15:52

6 Answers6

20

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']; 
}
Aditya Vikas Devarapalli
  • 3,186
  • 2
  • 36
  • 54
9

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.

display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
6

You cannot directly see the query result using mysql_query(). It just fires the query in mysql, nothing else.

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.

questionto42
  • 7,175
  • 4
  • 57
  • 90
Neeraj Kumar
  • 1,058
  • 1
  • 9
  • 22
0

Strangely, none of the while loops in the other answers worked for me, they did not throw an error, but their echo was empty. It worked when using foreach instead:

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

questionto42
  • 7,175
  • 4
  • 57
  • 90
0

I had to change

$result = mysql_query($sql);

to

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

and then use the foreach (thanks to 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

ACKmiecik
  • 70
  • 8
0
$query ="
  SELECT 
   *
  FROM
    users
";
die($query);

In this case your query print on the browser copy this query and run on SQL Server.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459