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?

1
  • 2
    Please stop using mysql_ functions, it's outdated and obsolete. Use mysqli or PDO. Commented Oct 13, 2014 at 12:56

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+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.
0

Try while loop

$sql = mysql_query("SELECT * FROM data ORDER BY id DESC");
while($row = mysql_fetch_array($sql))
{
  print_r($row);
}

Comments

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);

Comments

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.