
Untitled
By: a guest on
May 8th, 2012 | syntax:
None | size: 1.87 KB | hits: 13 | expires: Never
How to restrict access to mysql connection from other classes?
<?php
class Core
{
function connect()
{
$db = @mysql_connect($host, $user, $pass);
@mysql_select_db($base, $db);
}
function query($sql)
{
return mysql_query($sql);
}
}
global $c;
$c = new Core();
include('plugin.php');
$p = new Plugin();
echo $p->not_work_connection();
echo $p->work_connection();
?>
<?php
class Plugin
{
function not_work_connection()
{
$sql = 'SELECT * FROM `net_country` LIMIT 0 , 1';
$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
return print_r($row, 1);
}
}
function work_connection()
{
global $c;
$result =$c->query('SELECT * FROM `net_country` LIMIT 0 , 1');
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
return print_r($row, 1);
}
}
}
?>
<?php
class Core
{
private $db;
function connect()
{
$this->db = @mysql_connect($host, $user, $pass);
@mysql_select_db($base, $db);
//dummy
@mysql_connect();
}
function query($sql)
{
//notice second param
return mysql_query($sql, $this->db);
}
}
global $c;
$c = new Core();
include('plugin.php');
$p = new Plugin();
echo $p->not_work_connection(); //doing a mysql_query will use the dummy resource and fail
echo $p->work_connection();
?>
include_once("class/sql.php")
$db = new DB();
include_once("class/table.php")
$table = new TableClass();
$result =$db->query($table->load(parameters));
while($row = mysql_fetch_array($result))
{
return print_r($row, 1);
}