Select Data From Table using PDO in PHP

How to select data from a table using PDO?

Explanation

SELECT command is used to retrieve values from a table.
In PDO there are two ways to query the databas, like
  • query() -- This method is to query/retrive PDO from the table. No parameters passed.
  • prepare() -- This method prepares a statement for execution while using the exec() method and returns a statement object. The method is used for querying data securely.

Syntax :

SELECT column_name(s) FROM table_name

Example :

pdo-select
Select query for the above table
$sql= "SELECT first_name, email FROM user_details";
$stmt = $db->query($sql);

In the above example we select or retrieve only the first_name and email from the user_details table. The output for the above will be like this
Result :
pdo-select1

To select all the values in the table use the following query
$sql= "SELECT * FROM user_details";
$stmt = $db->query($sql);

Result :
pdo-select

PHP Topics


Ask Questions

Ask Question