AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQLi order by


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP MySQLi order by

ORDER BY sort the result-set in ascending or descending order.

ORDER BY is used to sorts the records in ascending order by default. Sorting records in descending order, you have to use the DESC keyword.

Syntax:

SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC

In this chapter as well as in the following chapters we will show you three methods of working with PHP MySQLi ORDSER BY:

PHP mysql order by alphabetical Last Name

ID First Name Last Name UserName Email
1 Clark Kent clarkkent [email protected]
2 John Carter johncarter [email protected]
3 Peter Parker peterparker [email protected]

 

PHP MySQLi ORDER BY MySQLi (object oriented)

The following examples rows will be ordered by the Last Name (last_name) column::

Example (this is HTML - PHP editor, change text on this window)

<?php

$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";

// Create connection
$link = new mysqli($server_name, $user_name,
$password, $db_name);

if ( $link->connect_error) {
die("Connection failed: " . $link->connect_error);
}

$sql = "SELECT id, first_name, last_name FROM Users ORDER BY last_name";
$result = $link->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "<br />";
}
} else {
echo "0 results";
}

$link->close();
?>

Note: When you create a new database, you must specify only: server_name, user_name and password).

num_rows() checks if there are more than zero rows returned.
fetch_assoc() puts all the results into an associative array that we can loop through.
while() loops through the result set and outputs the data from table "Users" the id, first-name and last-name columns.

Related subjects:
PHP myadmin where PHP myadmin update date Insert multiple rows PHP select / view data Conect to MySQLi

Tags: php mysql order by multiple columns, order by ascending mysql, sorting data from database in php, mysql order by specific sequence, php order by desc, mysql order by date, mysql descending order, mysql order by desc limit,
PHP MySQL order by, asc desc, multiple columns, alphabetical, date descending, desc limit 1

 

PHP MySQLi ORDER BY MySQLi (Procedural)

Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password")

The following examples rows will be ordered by the Last Name (last_name) column - MySQLi (Procedural)

Example (this is HTML - PHP editor, change text on this window)

<?php

$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";

// Create connection
$link = mysqli_connect($server_name, $user-name, $password, $db-name);
// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, firs_tname, last_name FROM Users ORDER BY last_name";
$result = mysqli_query($link, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "<br />";
}
} else {
echo "0 results";
}

mysqli_close($link);
?>

 

PHP MySQLi ORDER BY using (PDO) extension.

PDO extension. Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password")

The next PDO example ORDER BY last_name alphabetical and it will be displayed in an HTML table.

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";

try {
$link = new PDO("mysql:host=$server_name;dbname=$db_name", $user_name, $password);

// Set the PDO error mode to exception
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}



// Attempt select query execution
try{
$sql = "SELECT * FROM Users ORDER BY last_name";
$result = $link->query($sql);
if($result->rowCount() > 0){
echo "<table align='center' style='width:99%'>";
echo "<tr style='background:#0C6; padding:8px'>";
echo "<th style='padding:8px'>id</th>";
echo "<th style='padding:8px'>first_name</th>";
echo "<th style='padding:8px'>last_name</th>";
echo "<th style='padding:8px'>email</th>";
echo "</tr>";
while($row = $result->fetch()){
echo "<tr style='border:thin #FFF; background: #CCC'>";
echo "<td style='padding:8px'>" . $row['id'] . "</td>";
echo "<td style='padding:8px'>" . $row['first_name'] . "</td>";
echo "<td style='padding:8px'>" . $row['last_name'] . "</td>";
echo "<td style='padding:8px'>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";



// Free result set
unset($result);
} else{
echo "No records matching your query were found.";
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}



// Close connection
unset($link);
?>

PHP mysql order by alphabetical Last Name - the outcome

ID First Name Last Name UserName Email
1 John Carter johncarter [email protected]
2 Clark Kent clarkkent [email protected]
3 Peter Parker peterparker [email protected]

 



php mysql order by multiple columns, order by ascending mysql, sorting data from database in php, mysql order by specific sequence, php order by desc, mysql order by date, mysql descending order, mysql order by desc limit,
PHP MySQL order by, asc desc, multiple columns, alphabetical, date descending, desc limit 1
PHP MySQLi order by - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...