phpcoonect to db

Solutions on MaxInterview for phpcoonect to db by the best coders in the world

showing results for - "phpcoonect to db"
Isabella
02 May 2019
1<?php
2
3class db {
4
5    public $host = "localhost"; //host name
6    public $user = "root";//database user name
7    public $password = "";//database password if set  
8    public $database = "database_name"; //database name
9    public $con;
10
11    public function Connect() {
12        $this->con = mysqli_connect($this->host, $this->user, $this->password, $this->database);
13
14        if (mysqli_errno($this->con)) {
15            echo 'Error: ' . mysqli_connect_error();
16            exit();
17        }
18    }
19
20    public function Disconnect() {
21        mysqli_close($this->con);
22    }
23
24    /**
25     * ExecuteQuery function
26     * @param string $sql Proivde your sql statements here.
27     * @param int $type 1:Select, 2:Insert, 3:Update
28     * @return boolean
29     */
30    public function ExecuteQuery($sql, $type = 1) {
31        $returnData;
32        try {
33            $sql = str_replace("''", "null", $sql);
34            $this->Connect();
35            //$sql = mysqli_escape_string($this->con, $sql);
36            $data = mysqli_query($this->con, $sql);
37
38            if ($type == 1) {
39                $returnData = $data;
40            }
41            if ($type == 2) {
42                $returnData = mysqli_insert_id($this->con);
43            }
44            if ($type == 3) {
45                $returnData = mysqli_affected_rows($this->con);
46            }
47
48            $this->Disconnect();
49        } catch (Exception $exe) {
50            $returnData = FALSE;
51        }
52        return $returnData;
53    }
54
55}
56
similar questions
queries leading to this page
phpcoonect to db