2017 © Pedro Peláez
 

library mysql-pdo-class

Just another PHP class to cope with MySQL via PDO.

image

qrzysio/mysql-pdo-class

Just another PHP class to cope with MySQL via PDO.

  • Saturday, May 5, 2018
  • by Qrzysio
  • Repository
  • 1 Watchers
  • 1 Stars
  • 99 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 10 % Grown

The README.md

Intro

Just another PHP class to cope with MySQL via PDO., (*1)

Installation

Use composer:, (*2)

composer require qrzysio/mysql-pdo-class

Usage

require 'vendor/autoload.php';

$db = new Db();

$db->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
$db->bind(':fname', 'John');
$db->bind(':lname', 'Smith');
$db->bind(':age', '24');
$db->bind(':gender', 'male');
$db->exec();

echo $db->lastId();

*** Transactions ***

$db->beginTrans();

$db->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
$db->bind(':fname', 'Jenny');
$db->bind(':lname', 'Smith');
$db->bind(':age', '23');
$db->bind(':gender', 'female');
$db->exec();


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

echo $db->lastId();

$db->endTrans();

*** Select a single row ***

$db->query('SELECT FName, LName, Age, Gender FROM mytable WHERE FName = :fname');
$db->bind(':fname', 'Jenny');
$row = $db->single();

*** Select multiple rows ***

$db->query('SELECT FName, LName, Age, Gender FROM mytable WHERE LName = :lname');
$db->bind(':lname', 'Smith');
$rows = $db->fetch();

echo $db->numrows();

Try { } catch { } usage

It's very convenient to use try and catch blocks., (*3)

try {
  $db->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
  $db->bind(':fname', 'Jenny');
  $db->bind(':lname', 'Smith');
  $db->bind(':age', '23');
  $db->bind(':gender', 'female');
  $db->exec();
} catch (PDOException $e) {
  echo '<p>Something went wrong!</p>';
  echo '<p><strong>Details:</strong></p>';
  echo '<p>'.$e->getMessage().'</p>';  
}

Or more useful method., (*4)

try {

  // doing some operations
  ValidateFirst();

  // adding to database
  try {
    $db->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
    $db->bind(':fname', 'Jenny');
    $db->bind(':lname', 'Smith');
    $db->bind(':age', '23');
    $db->bind(':gender', 'female');
    $db->exec();    
  } catch (PDOException $e) {
    throw new Exception('We have an issue with this code!');    
  }

  // additional operations
  DoSomehing();

} catch (Exception $e) {
  echo '<p>Something went wrong!</p>';
  echo '<p><strong>Details:</strong></p>';
  echo '<p>'.$e->getMessage().'</p>';  
}

All methods

exec() - execute SQL query, (*5)

fetch() - fetch all rows and returns an array, (*6)

single() - fetch only one row as an array, (*7)

numrows() - returns number of rows affected or counted, (*8)

lastId() - last inserted ID, (*9)

cancel() - rollback the operation, (*10)

debug() - returns debugDumpParams() method from PDO, (*11)

beginTrans() - begins a transaction, (*12)

endTrans() - ends a transaction  , (*13)

License

This script was made by a bloger and published here: http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/, (*14)

There is no info about license, so I believe it's MIT. I did some small changes in the code and perhaps will do more in the future. According to the former author, license of this script is MIT., (*15)

The Versions

05/05 2018

dev-master

9999999-dev

Just another PHP class to cope with MySQL via PDO.

  Sources   Download

MIT

The Requires

  • php >=5.1.0

 

php pdo mysql class

05/05 2018

v1.0.7

1.0.7.0

Just another PHP class to cope with MySQL via PDO.

  Sources   Download

MIT

The Requires

  • php >=5.1.0

 

php pdo mysql class

05/05 2018

v1.0.6

1.0.6.0

Just another PHP class to cope with MySQL via PDO.

  Sources   Download

MIT

The Requires

  • php >=5.1.0

 

php pdo mysql class

04/08 2016

v1.0.5

1.0.5.0

Just another PHP class to cope with MySQL via PDO.

  Sources   Download

MIT

The Requires

  • php >=5.1.0

 

php pdo mysql class

04/06 2016

v1.0.4

1.0.4.0

Just another PHP class to cope with MySQL via PDO.

  Sources   Download

MIT

The Requires

  • php >=5.1.0

 

php pdo mysql class

19/05 2016

v1.0.3

1.0.3.0

Just another PHP class to cope with MySQL via PDO.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

php pdo mysql class

19/05 2016

v1.0.2

1.0.2.0

Just another PHP class to cope with MySQL via PDO.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

php pdo mysql class

19/05 2016

v1.0.1

1.0.1.0

Just another PHP class to cope with MySQL via PDO.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

php pdo mysql class

19/05 2016

v1.0.0

1.0.0.0

Just another MySQL/PDO class (PHP).

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

php pdo mysql class