diff --git a/database/PDO/README.md b/database/PDO/README.md new file mode 100644 index 0000000..ed94a02 --- /dev/null +++ b/database/PDO/README.md @@ -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);` \ No newline at end of file diff --git a/database/PDO/config.php b/database/PDO/config.php new file mode 100644 index 0000000..bddbba5 --- /dev/null +++ b/database/PDO/config.php @@ -0,0 +1,16 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + //echo "Connected successfully"; +} catch(PDOException $e) { + echo "Connection failed: " . $e->getMessage(); +} + +?> diff --git a/database/PDO/index.php b/database/PDO/index.php new file mode 100644 index 0000000..91e32a9 --- /dev/null +++ b/database/PDO/index.php @@ -0,0 +1,17 @@ +'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; + } +?> \ No newline at end of file