Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Connection
- {
- public $dbhost = "localhost";
- public $dbdriver = "mysql";
- public $dbuser = "root";
- public $dbpass = "usbw";
- public $dbname = "demo";
- public $charset = "utf8mb4";
- public $dbport = "3306";
- public $pdo;
- public $notification;
- public function __construct()
- {
- if (!isset($this->pdo)) {
- try {
- $this->pdo = new PDO($this->dbdriver . ":host=" . $this->dbhost . ";port=" . $this->dbport . ";dbname=" . $this->dbname . ";" . $this->charset, $this->dbuser, $this->dbpass, array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- PDO::ATTR_PERSISTENT => true
- ));
- if (!empty($this->pdo)) {
- $this->notification = "Connected";
- }
- }
- catch (PDOException $e) {
- echo $e->getMessage();
- }
- }
- }
- public function insert($table,$data){
- if (!empty($table) && !empty($data) && is_array($data)) {
- $column = "";
- $value = "";
- $i = 0;
- if (!array_key_exists("created", $data)) {
- $data['created'] = date("Y-m-d H-i-s");
- }
- if (!array_key_exists("modified", $data)) {
- $data['modified'] = date("Y-m-d H-i-s");
- }
- foreach ($data as $key => $value) {
- $pre = ($i > 0)?",":"";
- $column = $pre.$key;
- $value = $pre."'".$value."'";
- $i++;
- }
- $query = "INSERT INTO ".$table." (".$column.") VALUES (".$value.")";
- $insert = $this->pdo->query($query);
- return $insert?$this->pdo->insert_id:false;
- }else{
- return false;
- }
- }
- SELECT column1, column2 FROM table_name WHERE condition1 AND condition2 AND condition3 ORDER BY Country ASC, CustomerName DESC LIMIT 0, 10 OFFSET 2;
- public function get_row ($table,$condition = array()){
- $sql = " SELECT ";
- $sql .= array_key_exists("select", $condition)?$condition["select"]:" * ";
- $sql .= " FROM ".$table;
- if (array_key_exists("where", $condition)) {
- $sql . = " WHERE ";
- $i =0;
- foreach ($condition['where'] as $key => $value) {
- $pre = ($i > 0 )?" AND ":"";
- $sql .=$pre.$key." = '".$value."'";
- $i++;
- }
- }
- if (array_key_exists("order_by", $condition)) {
- $sql .= " OREDR BY ".$condition['id'];
- }
- if (array_key_exists("start", $condition) && array_key_exists("limit", $condition) && array_key_exists("offset", $condition)) {
- $sql .=" LIMIT ".$condition['start'].",".$condition['limit']." OFFSET ".$condition['offset'];
- }
- elseif (!array_key_exists("start", $condition) && array_key_exists("offset", $condition)) {
- $sql .=" LIMIT ".$condition['limit']." OFFSET ".$condition['offset'];
- }
- elseif (!array_key_exists("start", $condition) && !array_key_exists("offset", $condition) && array_key_exists("limit", $condition)) {
- $sql .=" LIMIT ".$condition['limit'];
- }
- // select
- $result = $this->pdo->query($sql);
- if (array_key_exists("return_type", $condition) && $condition['return_type'] != "all") {
- switch ($condition['return_type']) {
- case "count":
- $data = $result->num_rows;
- break;
- case "single":
- $data = $result->fetch_assoc();
- break;
- default:
- $data = "";
- }
- }
- else {
- if ($result->num_rows) > 0 {
- while ($row = $result->fetch_assoc()) {
- $data[]=$row;
- }
- }
- }
- return !empty($data)?$data:false;
- }
- $data = [
- 'name' => $name,
- 'surname' => $surname,
- 'sex' => $sex,
- ];
- $stmt = $this->pdo->prepare ("INSERT INTO user (firstname, surname) VALUES (:f-name, :s-name)");
- $stmt -> bindParam(':f-name', 'John');
- $stmt -> bindParam(':s-name', 'Smith');
- $stmt -> execute();
- public function insert ($table,$data){
- if (!empty($data) && is_array($data)) {
- $columns = "";
- $values = "";
- $i = 0;
- foreach ($data as $key => $value) {
- $pre = ($i > 0)?",":"";
- $columns =$pre.$key;
- $values = $pre."'".":".$value."'";
- $i++;
- }
- $query = " INSERT INTO ".$table." (".$columns.") VALUES (".$values.")";
- $stmt = $this->pdo->prepare($query);
- $bindcolumn = "";
- $bindvalue = "";
- foreach ($data as $keyb => $valueb) {
- $stmt ->bindParam (':'.$keyb, $valueb);
- }
- return $stmt -> execute();
- }
- else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement