Home ORDER BY is not working for mysql table
Reply: 2

ORDER BY is not working for mysql table

Rohan Akut
Rohan Akut Published in 2018-01-12 23:54:56Z

I have a dummy database saved in phpmyadmin.I am trying to sort the data on the basis of column user_uid. Here is some of my database

The php code that i am using to sort the table is

include_once 'dbh.inc.php';//file contains the variables req to connect to database
$sql = "SELECT * FROM client 
        ORDER BY user_uid;";
$result = mysqli_query($conn,$sql);
    $row = array();
    ($row = mysqli_fetch_array($result));

However this doesn't seem to work.What am I doing wrong with the code?

Additional info: user_uid is a foreign key and the reference to the user_uid is from another table named users

aendeerei Reply to 2018-01-13 00:41:15Z

Your code shows only one record. Here is a working example making use of the needed while loop to display the client_name of each fetched record:

include_once 'dbh.inc.php';

$sql = 'SELECT * FROM client 
        ORDER BY user_uid';

$result = mysqli_query($conn, $sql);

if ($result->num_rows > 0) {
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        echo $row['client_name'] . '<br>';

I removed all the unneeded parenthesis around the vars and the semicolon at the end of the sql statement.

Good luck.

Edit 1: I corrected $connection to $conn.

Edit 2: I implemented the check on the number of rows ($result->num_rows > 0) instead of on the $result itself (which is always an object of type mysqli_result and whos check therefore always returns true - even when no records are found).

infinitezero Reply to 2018-01-12 23:56:45Z

Well in your code you have written

ORDER BY cost_per_session

Just change it to

ORDER BY user_id

you can use ASC or DESC for ascending or descending order.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30148 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO