0

here is code i am trying to display all data from database...

$sql = mysql_query("SELECT * FROM data ORDER BY id DESC");
$row1 = mysql_fetch_array($sql);
print_R($row1);

but it is displaying only one row.. I have totally three rows in database , if i run the same query in database, it displays all rows.. i want to display all three rows how to fix this?

Your Friend
  • 1,119
  • 3
  • 12
  • 27

4 Answers4

6

Since you're expecting multiple rows you need to loop them using while:

$sql = mysql_query("SELECT * FROM data ORDER BY id DESC");
while($row1 = mysql_fetch_assoc($sql)) {
    echo $row1['column_name'];
}

Obligatory Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Here is a simple example coming from PDO's counterpart:

$con = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password'); // simple connection
$query = $con->query('SELECT * FROM data ORDER BY id DESC'); // put your query here
while($row = $query->fetch(PDO::FETCH_ASSOC)) { // same concept, you need to fetch them and put them inside the while loop since you're expecting multiple rows
    echo $row['column_name'];
}

Basically the first answer will work but I urge you to ditch that deprecated API and start using PDO. Don't worry, you won't have shortage of sources in learning this API.

Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • 2
    +1 for the "_Please, don't use mysql_* functions **in new code**._" instead of the widespread for OP's often useless and not very constructive "_mysql_* is deprecated_", which only is the case for new PHP versions, <4% of the servers worldwide. – davidkonrad Oct 13 '14 at 12:59
0

Try while loop

$sql = mysql_query("SELECT * FROM data ORDER BY id DESC");
while($row = mysql_fetch_array($sql))
{
  print_r($row);
}
Manwal
  • 23,450
  • 12
  • 63
  • 93
0

u need to loop ur query,

$sql = mysql_query("SELECT * FROM data ORDER BY id DESC");
$row1 = mysql_fetch_array($sql);
print_R($row1);

// wrong one

use mysqli,

$con=mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Perform query

      $sql = mysqli_query($con,"SELECT * FROM data ORDER BY id DESC");
        $mainarr=array();    
    while($row = mysql_fetch_assoc($sql)) {
        $subarr=array();
        $subarr['key']=$row['your field'];
        array_push($mainarr,$subarr);
    }  
print_r($mainarr);
Arun
  • 146
  • 9
0

its simple to use mysqli just try the following once

$mysqli   = new mysqli("localhost", "username", "password", "database");
$strr     = "SELECT * FROM data ORDER BY id DESC";  // its a good practice to use column name insted of *
$result   = $mysqli->query($strr);
while($arr      = $result->fetch_array())
{
  print_r($arr);
}
Ronser
  • 1,855
  • 6
  • 20
  • 38