Advertisement
Guest User

Untitled

a guest
Jun 7th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1.  
  2. <?php  
  3.     error_reporting(E_ALL);
  4.     ini_set('display_errors', 1);
  5.     ini_set('error_reporting', E_ALL);
  6.     ini_set('display_startup_errors', 1);
  7.     error_reporting(-1);   
  8.     class Database
  9.     {    
  10.         private $host = "localhost";
  11.         private $db_name = "test";
  12.         private $username = "root";
  13.         private $password = "";
  14.         public $conn;    
  15.         public function dbConnection()
  16.         {    
  17.             $this->conn = null;    
  18.             try
  19.             {
  20.                 $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
  21.                 $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);                          
  22.                 $this->conn->exec("SET CHARACTER SET `utf8`");
  23.             }
  24.             catch(PDOException $e)
  25.             {                  
  26.                 print_r("Connection error: " . $e->__toString());
  27.             }        
  28.             return $this->conn;
  29.         }
  30.     }  
  31.     $DataBase = new Database;      
  32.     $DataBase = $DataBase->dbConnection();
  33.     class IUD
  34.     {
  35.         public function __construct()
  36.         {
  37.             $database = new Database();
  38.             $db = $database->dbConnection();
  39.             $this->conn = $db;     
  40.         }  
  41.         public function Insert($Tbl, $Fild, $Data)
  42.         {
  43.             if(is_array($Fild))
  44.             {
  45.                 $Fild = implode(", ", $Fild);  
  46.                 $Data = implode(", ", $Data);              
  47.                 //print_r($Tbl."<br>".$Fild."<br>".$Data);             
  48.                 $Sql = "INSERT INTO `".$Tbl."` (".$Fild.") VALUES (".$Data.")";
  49.                 $Sql = $this->conn->prepare($Sql);
  50.                 $Sql->execute();
  51.                
  52.             }
  53.         }
  54.     }
  55.     $Test = new IUD;
  56.     $Fild = array("name", "family");
  57.     $Data = array("c","f");
  58.     $Test->Insert("fc", $Fild, $Data);
  59. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement