Created
May 14, 2013 04:49
-
-
Save wookiecooking/5573768 to your computer and use it in GitHub Desktop.
[PHP] really bad mysql OOP example, needs to be upgraded to PDO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class mSQL{ | |
| var $host; | |
| var $username; | |
| var $password; | |
| var $database; | |
| public $dub; | |
| public function connect($set_host, $set_username, $set_password, $set_database) | |
| { | |
| $this->host = $set_host; | |
| $this->username = $set_username; | |
| $this->password = $set_password; | |
| $this->database = $set_database; | |
| $this->dub = mysql_connect($this->host, $this->username, $this->password)or die("cannot connect"); | |
| mysql_select_db($this->database)or die("cannot select DB"); | |
| } | |
| public function safe($val) | |
| { | |
| return mysql_real_escape_string($val); | |
| } | |
| public function query($sql) | |
| { | |
| return mysql_query(safe($sql),$this->dub); | |
| } | |
| public function fetch($sql) | |
| { | |
| return mysql_fetch_array($this->query(safe($sql))); | |
| } | |
| public function salt($protect) | |
| { | |
| $saltdate = date("Y.m.d"); | |
| $salt = sha1(md5($saltdate)); | |
| $result = sha1(md5($protect).$salt); | |
| return $result; | |
| } | |
| } | |
| $connect = new mSQL(); | |
| // Creds & Database | |
| $connect->connect('localhost', 'user', 'pass', 'database1'); | |
| // Using the built-in mysql_query function with the class connection | |
| $query = mysql_query(safe("SELECT * FROM table"), $connect->dub); | |
| $msqlarray = mysql_fetch_array($query); | |
| // Using query function inside the class | |
| $query = $connect->query("SELECT * FROM table"); | |
| $msqlarray = mysql_fetch_array($query); | |
| // Or Simply use the fetch function to return with mysql_fetch_array already. | |
| $msqlarray = $connect->fetch("SELECT * FROM table"); | |
| echo $msqlarray['row']; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment