Show DevBest [PHP]Simple MySQL class , you can use it for CMSes using MySQL

Status
Not open for further replies.

Mac

New Member
Feb 9, 2011
111
2
PHP:
<?php
class MySQL {
	public $host;
	public $user;
	public $pass;
	public $dbname;
	public function __construct($a, $b, $c, $d) {
		if(isset($a, $b, $c, $d)):
			$this->host = $a;
			$this->user = $b;
			$this->pass = $c;
			$this->dbname = $d;
		else:
			throw new exception('Required fields are not set');
		endif;
		if(isset($this->host, $this->user, $this->pass, $this->dbname)):
			mysql_connect($this->host, $this->user, $this->pass) or die(mysql_error());
			mysql_select_db($this->dbname) or die(mysql_error());
		else:
			throw new exception('Required fields for MySQL connection or MySQL selecting db are not set');
		endif;
	}
	public function makeQuery($e) {
		if(isset($e)):
			mysql_query($e);
		else:
			throw new exception('MySQL query is empty');
		endif;
	}
	public function num_rows($f) {
		if(isset($f)):
			mysql_num_rows($f);
		else:
			throw new exception('MySQL num_rows function is empty');
		endif;
	}
	public function mysql_array($g) {
		if(isset($g)):
			mysql_fetch_array($g);
		else:
			throw new exception('MySQL mysql_array function is empty');
		endif;
	}
	public function escape_string($h) {
		if(isset($h)):
			mysql_escape_string($h);
		else:
			throw new exception('MySQL escape_string function is empty');
		endif;
	}
}
$mysql = new MySQL("localhost", "root", "pass", "dbname");
 

Mac

New Member
Feb 9, 2011
111
2
yes , i have tested it . lemme add a variable too for connecting it to class because some peoples doesn't understand construct thingy!

edit{
kk , done
}
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Very nice, the only thing you would have to change is:
PHP:
mysql_fetch_array($g) to mysql_fetch_array(mysql_query($g))

I think it isn't very smart to use a function for each type of query, and you could make it so you can do all types in 1 single function.
 

Mac

New Member
Feb 9, 2011
111
2
Kryptos {
I don't think it should be changed because for example :
PHP:
$sql = mysql_query("SELECT * FROM news"); while($xd = $mysql->mysql_array($sql)):
echo $xd['title'];
endwhile;
thats why! anyways if string of sql or wha ever variable will be only the string of the query then .. your right!
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
No, because you can do:
PHP:
$news = "SELECT * FROM news";
while($row = $mysql->mysql_array($sql))
{
echo $row['title'];
}
So, it would work always.
 

Mac

New Member
Feb 9, 2011
111
2
I just said that ?

thats why! anyways if string of sql or wha ever variable will be only the string of the query then .. your right!
 
Status
Not open for further replies.

Users who are viewing this thread

Top