List

Try: ca/tecreations/ViewFile.php


<?php

class MySQL {
    var $mysqli;
    var $connected;
    var $debug = false;
        
    public function __construct($host = "", $user = "", $pass = "", $db = "") {
	if (isset($_SESSSION['level']) && $_SESSION['level'] == 9) {
            $this->debug = true;
        } else $this->debug = false;
        //echo "Connecting as: $user Using: $pass To $host.$db";
        if (!isset($this->mysqli)) {
            $this->mysqli = new mysqli($host,$user,$pass,$db);
            //print("Host: $host, User: $user, Pass: '$pass',Database: $db");
    	}
    	// If connection was not successful, handle the error
        if (!$this->mysqli) {
            print("Host: $host, User: $user, Pass: '$pass',Database: $db");
        } else {
            $this->connected = true;
            if ($db != "") $this->issue("USE $db");
            return $this->mysqli;
    	} 
    	if ($this->mysqli->connect_errno) {
            $this->connected = false;
            if (mysqli_connect_errno() == 2002) {
                die("Can't connect in MySQL.php... did you 'setenforce 0'?");
            }
            // print the error
            echo "Error : " . $this->mysqli->connect_error . "<br />\n";
            // and our connection parameters
            echo "Host: $host, User: $user, Pass: '$pass', Database: $db";
    	} 
        return false;
    }
	
    public function connected() { return $this->connected; }
	
    public function grantPrivileges($host = "", $user = "", $pass = "", $name = "") {
        global $db;
        if ($db) {
            if (!$db->issue("GRANT ALL PRIVILEGES ON $name.* TO '$user'@'tecreations.ca' IDENTIFIED BY '$pass'")) {
                return false;
                //die("couldn't grant...");
            }
            if (!$db->issue("GRANT ALL PRIVILEGES ON $name.* TO '$user'@'localhost' IDENTIFIED BY '$pass'")) {
                return false;
                //die("couldn't grant...");
            }
            if (!$db->issue("FLUSH PRIVILEGES")) {
                return false;
                //die("couldn't flush...");
            }
            return true;
        } else echo "<font color='red'>Error Database (\$db) is null.</font><br />\n";
    }

    public function issue($sql = "") { return $this->query($sql); }
        
    public function query($sql = "") {
	if ($this->debug) print("SQL: $sql<br />\n");
	$result = $this->mysqli->query($sql);
        if ($this->getError() != "") {
            print("MySQL: $sql<br /><font color='red'>Error: " . $this->getError() . "</font>");
        }
        return $result;
    }

    public function debugQuery($sql = "") {
	global $debug;		
	if ($this->debug) {
            print("<br />\n");
            print("SQL: '$sql'<br />\n");
            print("MySQL: $sql<br /><font color='red'>Error: " . $this->getError() . "</font>");
            print("<br />\n");
	}
    }
	
    public function getError() {
	return $this->mysqli->error;
    }
	
    public function select($sql = "") {
        $rows = array();
	$result = $this->query($sql);
	
	// If query failed, return `false`
	if ($result === false) {
            $this->debugQuery($sql);
            return false;
        }
	
	// If query was successful, retrieve the count and all the rows into an array
        $count = mysqli_num_rows($result);
	while ($row = $result->fetch_assoc()) {
            $rows[] = $row;
	}
        if ($count == 0) return array("count" => "0", "rows" => "");
        else if ($count == 1) {
            return array("count" => "1", "rows" => $rows[0]);
        } else {
            return array("count" => "$count", "rows" => $rows);
        }
    }
	
    public function quote($value) {
        return "'" . mysqli_real_escape_string($this->mysqli,$value) . "'";
    }

    public function setDebug($state = true) {
        $this->debug = $state;
    }
    
    public function hasDB($db_name = "") {
        $databases = $this->select("SHOW DATABASES");
        //print_r($databases);
        foreach($databases as $array) {
            if ($array['Database'] == $db_name) return true;
        }
        return false;
    }

    public function hasTable($db_name = "",$table_name = "") {
        if ($db_name == "") die("db_name is empty in call to MySQL->hasTable()");
        if ($table_name == "") die("table_name is empty in call to MySQL->hasTable()");
        $tables = $this->select("SHOW TABLES");
        if ($this->debug) {
            print("MySQL->hasTable($table_name)...");
            print_r($tables);
        }
        if ($tables['count'] > 0) {
            $rows = $tables['rows'];
            foreach($rows as $row) {
                if ($row["Tables_in_$db_name"] == $table_name) return true;
            }
        } else {
            return false;
        }
        return false;
    }
        

}