Skip to content

malek-academy/PDO-Database-Class-Connection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PDO-Database-Class-Connection

A PHP MySQL PDO class

Initialize

// Require Files;
require_once 'Config.php';
require_once 'PDO_Interface.php';
require_once 'PDO.Class.php';

// Create PDO_Connect Class

$db = new PDO_Connect();

How To Use

We will now demo the right ways to properly use this class and you can use this example
  • create a database with any name you want

    Then inject this SQL

    CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, `member` tinyint(4) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

simple query

<?php
	$query = $db->query('id', 'users')->execute();
?>

Use Insert , lastInsertId Medols

<?php
$db->insert('users', "`username` , `member`", ":username , :member")
            ->bind([
                'username'=>'Malek Academy',
                'member'=>1
                ])
            ->execute();

if($db->lastInsertId())    
    echo 'insert successfuly';
else
    echo 'insert failed';
?>

Use Update Medol

<?php
$db->update('users', "`username` = :username , `member` = :member", 'id', '1')
        ->bind(
                [
                    'username'  => 'Mohammd',
                    'member'    => 0
                ]
            );

if($db->execute())
    echo 'update successfuly';
else
    echo 'update failed';
?>

Use Delete Medol

<?php
$db->delete('users', 'id', ":id")->bind(['id' => 1]);

if($db->execute())
    echo 'delete successfuly';
else
    echo 'delete failed';
 ?>

Get Rows Count

get rows count with out binding
<?php
$rows_count = $db->query('id', 'users')->execute()->rowCount();

echo $rows_count;
?>

get rows count with binding Safety

<?php
$row_count = $db->query('id', 'users' , "WHERE `username` = :username")
                ->bind(
                      [
                        'username'=>$_GET['user']
                      ])
                ->execute()->rowCount();

echo $row_count;
?>

Use A single Fetch

<?php
$query = $db->query('*', 'users' , "WHERE `username` = :username")
                ->bind(
                      [
                        'username'=>$_GET['user']
                      ])
                ->execute()->fetch();

echo '<pre>';
print_r($query);
echo '</pre>';

?>

Use Fetch All

<?php
$query = $db->query('*', 'users' , "WHERE `member` = :member")
                ->bind(
                      [
                        'member'=>1
                      ])
                ->execute()->fetchAll();

echo '<pre>';
print_r($query);
echo '</pre>';

?>

Close Database Connection

<?php
$db->close();
?>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages