Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions database/PDO/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Database Connection Using PDO in PHP

## For Database Connection

`$conn = new PDO("mysql:host=hostname;dbname=dbname, username, password);`

## Prepare Statement for Query

`$statement = $connect->prepare($query);`

## Excute Query

`$statement->execute($data);`

## Count Row

`$total_row = $statement->rowCount();`

## Fetch Row

`$result = $statement->fetch(PDO::FETCH_ASSOC);`
16 changes: 16 additions & 0 deletions database/PDO/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
$hostname = "hostname";
$username = "username";
$password = "password";
$db="dbname";

try {
$connect = new PDO("mysql:host=$hostname;dbname=".$db, $username, $password);
// set the PDO error mode to exception
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

?>
17 changes: 17 additions & 0 deletions database/PDO/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
include('config.php');


$data=array(
':post_status' =>'active'
);

$query=" SELECT * FROM table_name WHERE post_status=:post_status";
$statement = $connect->prepare($query);
$statement->execute($data);
$total_row = $statement->rowCount();
if($total_row>0){
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result;
}
?>