Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- // Database connection
- private $servername = 'localhost';
- private $username = 'root';
- private $password = '';
- private $dbname = 'emp';
- function __construct()
- {
- $this->db = new mysqli(
- $this->servername,
- $this->username,
- $this->password,
- $this->dbname
- );
- if ($this->db->connect_error) {
- die("Connection failed: " . $this->db->connect_error);
- }
- }
- public function insert($table, $cols, $vals) { // insert function
- $colsString = implode(',', $cols);
- $valsString = implode(',', $vals);
- $sql = "INSERT INTO $table ($colsString) VALUES ($valsString)";
- if ($this->db->query($sql) === TRUE) {
- // return $this->Db->insert_id;
- return true;
- } else {
- return "Error: " . $sql . "<br>" . $this->db->error;
- }
- }
- public function select($table,$cols = array()) { // select function
- if(empty($cols)){
- $colsString = '*';
- }else{
- $colsString = implode(',', $cols);
- }
- $sql="select $colsString from $table";
- $result=$this->db->query($sql);
- while($row = $result->fetch_assoc()) {
- $result1[] = $row;
- }
- // $row = mysqli_fetch_all($result);
- return $result1;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment