Jump to content
prohor

How to show data from mysql database in decending order using pdo?

Recommended Posts

I am trying to show data using  "fetch(PDO::FETCH_desc" for show from descending order but it only works for assoc order like bellow code, how I can show it in descending order? I am new to php and pdo, please help.

$stmt = $DB_con->prepare(" SELECT name, id FROM class  ORDER BY id DESC  LIMIT 1, {$recordsPerPage}");
    $stmt->execute();
    echo $name;
   
    if($stmt->rowCount() > 0)
       
    {
        while($row=$stmt->fetch(PDO::FETCH_ASSOC))
        
        {
            extract($row);
            
            echo $name;
            
        }
    }
           

Share this post


Link to post
Share on other sites

If you want the names in descending order then "ORDER BY name DESC" in your query instead of id.

 

Avoid using extract(), it's too easy to overwrite variables. Instead

 

echo $row['name'];
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.