PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL PHP / PostgreSQL PHP: Delete Data From a Table

PostgreSQL PHP: Delete Data From a Table

Summary: this tutorial shows you how to delete data from a PostgreSQL table using the PHP PDO.

Steps for deleting data in the PostgreSQL using PHP PDO

To delete data from a PostgreSQL table in PHP, you use the following steps:

  1. Connect to the PostgreSQL database server by creating an instance of the PDO class.
  2. Prepare the DELETE statement for execution by calling the prepare() method of the PDO object. The prepare() method returns a PDOStatement object.
  3. Bind values to the DELETE statement by calling the bindValue() method of the PDOStatement object.
  4. Execute the DELETE statement by calling the execute() method.
  5. Get the number of rows deleted using the rowCount() method.

Deleting data examples

We will use the stocks table for the demonstration. If you have not created the stocks table yet, you can follow the creating table tutorial.

Let’s create a new class named StockDB that contains all the methods for deleting data in a table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
namespace PostgreSQLTutorial;
 
/**
* PostgreSQL PHP delete data demo
*/
class StockDB {
 
    /**
     * PDO object
     * @var \PDO
     */
    private $pdo;
 
    /**
     * Initialize the object with a specified PDO object
     * @param \PDO $pdo
     */
    public function __construct($pdo) {
        $this->pdo = $pdo;
    }
    // other methods
    // ...
}

The following delete() method deletes a row specified by id from the stocks table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   /**
     * Delete a row in the stocks table specified by id
     * @param int $id
     * @return the number row deleted
     */
    public function delete($id) {
        $sql = 'DELETE FROM stocks WHERE id = :id';
 
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindValue(':id', $id);
 
        $stmt->execute();
 
        return $stmt->rowCount();
    }

The following deleteAll() method deletes all rows from the stocks table.

1
2
3
4
5
6
7
8
9
10
   /**
     * Delete all rows in the stocks table
     * @return int the number of rows deleted
     */
    public function deleteAll() {
 
        $stmt = $this->pdo->prepare('DELETE FROM stocks');
        $stmt->execute();
        return $stmt->rowCount();
    }

Before running the methods, we query the data from the stocks table.

1
2
3
4
5
6
7
8
9
stocks=# SELECT * FROM stocks
stocks-# ORDER BY id;
id | symbol |        company
----+--------+-----------------------
  1 | MSFT   | Microsoft Corporation
  2 | GOOGL  | Alphabet Inc.
  3 | YHOO   | Yahoo! Inc.
  4 | FB     | Facebook, Inc.
(4 rows)

Use the following code in the index.php file to delete the row with id 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
 
require 'vendor/autoload.php';
 
use PostgreSQLTutorial\Connection as Connection;
use PostgreSQLTutorial\StockDB as StockDB;
 
try {
    // connect to the PostgreSQL database
    $pdo = Connection::get()->connect();
    //
    $stockDB = new StockDB($pdo);
    // delete a stock with a specified id
    $deletedRows = $stockDB->delete(1);
    echo 'The number of row(s) deleted: ' . $deletedRows . '<br>';
    
} catch (\PDOException $e) {
    echo $e->getMessage();
}

The following is the output:

1
The number of row(s) deleted: 1

We query data from the stocks table again to verify.

1
2
3
4
5
6
7
8
stocks=# SELECT * FROM stocks
stocks-# ORDER BY id;
id | symbol |    company
----+--------+----------------
  2 | GOOGL  | Alphabet Inc.
  3 | YHOO   | Yahoo! Inc.
  4 | FB     | Facebook, Inc.
(3 rows)

The row with id 1 was deleted as expected.

In the index.php file, modify the code to call the deleteAll() method instead of the delete() method and execute it. The following is the output of the script:

1
The number of row(s) deleted: 3

The following shows the output when we query data from the stocks table.

1
2
3
4
5
stocks=# SELECT * FROM stocks
stocks-# ORDER BY id;
id | symbol | company
----+--------+---------
(0 rows)

All rows in the stocks table have been deleted as expected.

In this tutorial, we have shown you how to delete data from a PostgreSQL table in PHP application using PDO API.

Previous Tutorial: PostgreSQL PHP: Working with BLOB

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.