PHP code example of lincanbin / php-pdo-mysql-class

1. Go to this page and download the library: Download lincanbin/php-pdo-mysql-class library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lincanbin / php-pdo-mysql-class example snippets



define('DBHost', '127.0.0.1');
define('DBPort', 3306);
define('DBName', 'Database');
define('DBUser', 'root');
define('DBPassword', '');


$DB->query("SELECT * FROM fruit WHERE name=?", array($_GET['name']));


$DB->query("SELECT * FROM fruit WHERE name=".$_GET['name']);


$DB->query("SELECT * FROM fruit WHERE name=? and color=?",array('apple','red'));
$DB->query("SELECT * FROM fruit WHERE name=:name and color=:color",array('name'=>'apple','color'=>'red'));

Array
(
	[0] => Array
		(
			[id] => 1
			[name] => apple
			[color] => red
		)
)


$DB->query("SELECT * FROM fruit WHERE name IN (:fruits)",array(array('apple','banana')));

Array
(
	[0] => Array
		(
			[id] => 1
			[name] => apple
			[color] => red
		)
	[1] => Array
		(
			[id] => 2
			[name] => banana
			[color] => yellow
		)
)


$query = "SELECT * FROM fruit WHERE name IN (:fruits) AND color = :color";
// use multidimensional array as $params
$params = array(
	"color" => "red",
	"fruits" => array(
		"apple",
		"banana"
	)
);
$DB->query($query, $params);

Array
(
	[0] => Array
		(
			[id] => 1
			[name] => apple
			[color] => red
		)
)


$DB->column("SELECT color FROM fruit WHERE name IN (:color)",array('apple','banana','watermelon'));

Array
(
	[0] => red
	[1] => yellow
	[2] => green
)


$DB->row("SELECT * FROM fruit WHERE name=? and color=?",array('apple','red'));

Array
(
	[id] => 1
	[name] => apple
	[color] => red
)


$DB->single("SELECT color FROM fruit WHERE name=? ",array('watermelon'));

green


// Delete
$DB->query("DELETE FROM fruit WHERE id = :id", array("id"=>"1"));
$DB->query("DELETE FROM fruit WHERE id = ?", array("1"));
// Update
$DB->query("UPDATE fruit SET color = :color WHERE name = :name", array("name"=>"strawberry","color"=>"yellow"));
$DB->query("UPDATE fruit SET color = ? WHERE name = ?", array("yellow","strawberry"));
// Insert
$DB->query("INSERT INTO fruit(id,name,color) VALUES(?,?,?)", array(null,"mango","yellow"));//Parameters must be ordered
$DB->query("INSERT INTO fruit(id,name,color) VALUES(:id,:name,:color)", array("color"=>"yellow","name"=>"mango","id"=>null));//Parameters order free


$DB->lastInsertId();


$DB->querycount;


$DB->closeConnection();


try {
    $DB->beginTransaction();
    var_dump($DB->inTransaction()); // print "true"
    $DB->commit();
} catch(Exception $ex) {
    // handle Error
    $DB->rollBack();
}

$iteratorInstance = $DB->iterator("SELECT * FROM fruit limit 0, 1000000;");
$colorCountMap = array(
    'red' => 0,
    'yellow' => 0,
    'green' => 0
);
foreach($iteratorInstance as $key => $value) {
    sendDataToElasticSearch($key, $value);
    $colorCountMap[$value['color']]++;
}
var_export($colorCountMap);

array(3) {
  [red] => 2
  [yellow] => 2
  [green] => 1
}

composer