Back to Top

To Swap Database Row in PHP

To Swap Database Row in PHP

Today I am going to show one more demo which is for to swap database table row like in Joomla for sorting or ordering a table.Here I am going to show you how to swap the id’s of rows of the particular table.

Basically, we are doing swapping to SWAP values of two variables with using a third variable so with the use this basic concept, I have created one demo which swap database rows with PHP coding.In order to switch the position of rows, you will need to set the Order(ASC, DESC) value of each row and then you can update rows with swap code.a

Let’s have a look into coding done by me so you get the idea how swapping of rows is possible.

Also READ: To use Flickr API with PHP

First of all create one table swap rows,

CREATE TABLE `swaprows` (
	`FirstName` VARCHAR( 255 ) NOT NULL ,
	`MiddleName` VARCHAR( 255 ) NOT NULL ,
	`LastName` VARCHAR( 255 ) NOT NULL

After creating table create one file swaprow.php and write a query to fetch records from the particular table you want to swap its row using MySQL functions.

$connection=mysql_connect("localhost","username","password") or die("Database Path is not valid");
mysql_select_db("Database_name",$connection) or die ("Database not found");
$query="select * from swaprows order by id ASC";

After select records from the database, It is a time to use records and for that you can create HTML as shown below:

$cnt = 1;
while($row=mysql_fetch_array($result)) {
     $last = mysql_num_rows($result);
First Name
Middle Name
Last Name

Next is to create one file that is validSwapID.php and the main important thing is to create one function which swap the row.Here I have written one function row_swap which swap database:

Recommended READ:
To Unzip file and move to destination in PHP
To grab content from nested tags in PHP

//To get Previous row id
$pid = $_GET['previd'];

//To get Next row id
$nid = $_GET['nextid'];

    $query="select * from swaprows where id <'".$_GET["previd"]."' ORDER BY id DESC LIMIT 1";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    $query="select * from swaprows where id >'".$_GET["nextid"]."' ORDER BY id ASC LIMIT 1";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
function row_swap($table, $id1, $id2)
     mysql_query("UPDATE $table SET id='999' where id='$id2'");
     // 999 is the third variable for swapping a two variable values.
     mysql_query("UPDATE $table SET id='$id2' where id='$id1'");
     mysql_query("UPDATE $table SET id='$id1' where id='999'");

Here above code, You will get previd and nextid. Using previd and nextid find out the previous and next row from swap rows Table.And after fetching the id, call the row_swap function which will update the row into the table and your data will be updated.

That’s it. Now you are ready to swap rows from the database.Have a look at the working Demo here.

View Demo

I hope you have understood the concept and it has been helpful to you.

Comments (3)

  1. What will happen with this code when your table has 1000 rows?

    Does that still works?

    1. You can change 999 value to other max value. Here I have used fix value and need to take third variable for swapping because i have swap primary key.

      any suggestion??

      1. .I’m very sure I didn’t msslpeil anything, it was just copied and pasted and only the number and category name changed. Any idea what can be the problem?Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to convert Object to array in PHP

Posted on 2 years ago


How to use Git init command

Posted on 1 year ago


The Reader’s Poll – June 2015

Posted on 3 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 4 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago