config.php

<?php

	define("DB_HOST", "localhost");
	define("DB_USER", "root");
	define("DB_PASS", "");
	define("DB_NAME", "test");

database.class.php

<?php

	class Database{

		private $host = DB_HOST;
		private $user = DB_USER;
		private $pass = DB_PASS;
		private $dbname = DB_NAME;

		private $dbh;
	    private $error;
	    private $stmt;
	 
	    public function __construct(){
	        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ';charset=utf8';
	        
	        $options = array(
	            PDO::ATTR_PERSISTENT    => true,
	            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION,
	            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
	        );
	        
	        try{
	            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
	        }catch(PDOException $e){
	            $this->error = $e->getMessage();
	        }
	    }

	    public function query($query){
		    $this->stmt = $this->dbh->prepare($query);
		}

		public function bind($param, $value, $type = null){
		    if (is_null($type)) {
		        switch (true) {
		            case is_int($value):
		                $type = PDO::PARAM_INT;
		                break;
		            case is_bool($value):
		                $type = PDO::PARAM_BOOL;
		                break;
		            case is_null($value):
		                $type = PDO::PARAM_NULL;
		                break;
		            default:
		                $type = PDO::PARAM_STR;
		        }
		    }
		    $this->stmt->bindValue($param, $value, $type);
		}

		public function execute(){
		    return $this->stmt->execute();
		}

		public function resultset(){
		    $this->execute();
		    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
		}

		public function single(){
		    $this->execute();
		    return $this->stmt->fetch(PDO::FETCH_ASSOC);
		}

		public function rowCount(){
		    return $this->stmt->rowCount();
		}

		public function lastInsertId(){
		    return $this->dbh->lastInsertId();
		}

		public function beginTransaction(){
		    return $this->dbh->beginTransaction();
		}

		public function endTransaction(){
		    return $this->dbh->commit();
		}

		public function cancelTransaction(){
		    return $this->dbh->rollBack();
		}

		public function debugDumpParams(){
		    return $this->stmt->debugDumpParams();
		}
	}

test.php

<?php

	include('config.php');
	include('database.class.php');

	$database = new Database();

	// INSERT A NEW RECORD
	$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');

	$database->bind(':fname', 'John');
	$database->bind(':lname', 'Smith');
	$database->bind(':age', '24');
	$database->bind(':gender', 'male');

	$database->execute();

	echo $database->lastInsertId();

	// INSERT MULTIPLE RECORDS USING A TRANSACTION
	$database->beginTransaction();

	$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');

	$database->bind(':fname', 'Jenny');
	$database->bind(':lname', 'Smith');
	$database->bind(':age', '23');
	$database->bind(':gender', 'female');

	$database->execute();

	$database->bind(':fname', 'Jilly');
	$database->bind(':lname', 'Smith');
	$database->bind(':age', '25');
	$database->bind(':gender', 'female');

	$database->execute();

	echo $database->lastInsertId();

	$database->endTransaction();

	// UPDATE RECORDS
	$database->query('UPDATE mytable SET FName = :fnname, LName = :lname WHERE ID = :id');

	$database->bind(':id', 3);
	$database->bind(':fname', 'John');
	$database->bind(':lname', 'Brown');

	$database->execute();

	echo $database->rowCount();

	// DELETE RECORDS
	$database->query('DELETE FROM mytable WHERE ID = :id');

	$database->bind(':id', 1);

	$database->execute();

	echo $database->rowCount();

	// SELECT A SINGLE ROW
	$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE FName = :fname');

	$database->bind(':fname', 'Jenny');

	$row = $database->single();

	echo "<pre>";
	print_r($row);
	echo "</pre>";

	// SELECT MULTIPLE ROWS
	$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE LName = :lname');

	$database->bind(':lname', 'Smith');

	$rows = $database->resultset();

	echo "<pre>";
	print_r($rows);
	echo "</pre>";

	echo $database->rowCount();

Comments

  1. Pingback: Online businesses

  2. Pingback: ฝาก ถอนเงิน สล็อตวอเลท ไม่มีขั้นต่ำ

  3. Pingback: รับทำบัญชี

  4. Pingback: beer789

  5. Pingback: locksmith Long Beach

  6. Pingback: Ventilatie depozite

  7. Pingback: budapest weed

  8. Pingback: ถุงคาร์บอน

  9. Pingback: raja bandarq

  10. Pingback: aksara178

  11. Pingback: ปั้มฟอล

  12. Pingback: ติดเน็ตบ้าน

  13. Pingback: additional info

  14. Pingback: buy runtz weed

  15. Pingback: bonanza 178

  16. Pingback: snus

  17. Pingback: ทินเนอร์คุณภาพสูง

  18. Pingback: ขายบ้านพัทยา

  19. Pingback: villas in phuket thailand with private pool

  20. Pingback: ล่ามภาษาญี่ปุ่น

  21. Pingback: judi togel

  22. Pingback: ทำความรู้จักกับ หุ้นนิเคอิ หรือ ดีชนีนิเคอิ

  23. Pingback: สมุนไพรภู่หลาน สรรพคุณ

  24. Pingback: โบรกเกอร์ Forex

  25. Pingback: ห้องพักรายวันรามอินทรา

  26. Pingback: slot gacor

  27. Pingback: truffle chocolate vs mushroom

  28. Pingback: สล็อตเว็บตรง

  29. Pingback: รับเทคอนกรีต สมุทรสาคร

  30. Pingback: goatpg

  31. Pingback: hpl laminate decorative

  32. Pingback: aviator game pin up

  33. Pingback: https://betwinnermx.com/

  34. Pingback: ufabtb

  35. Pingback: http://www.hcceskalipa.cz/bazarek/ridiculously-simple-ways-improve-your-hmcasinosperucomcasino21-casino-review

  36. Pingback: my latest blog post

  37. Pingback: การรักษามะเร็ง

  38. Pingback: bonanza178

  39. Pingback: 무료웹툰

  40. Pingback: ทดลองเล่นสล็อต pg

  41. Pingback: how to install vinyl cornhole wraps tutorial

  42. Pingback: buy oxycodone pills online

  43. Pingback: เสือมังกร lsm99

  44. Pingback: https://www.toasterjeep.com/proxy.php?link=https://gg4.store/