Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.87 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to restrict access to mysql connection from other classes?
  2. <?php
  3.     class Core
  4.     {
  5.         function connect()
  6.         {
  7.             $db = @mysql_connect($host, $user, $pass);
  8.             @mysql_select_db($base, $db);
  9.         }
  10.  
  11.         function query($sql)
  12.         {
  13.             return mysql_query($sql);
  14.         }
  15.     }
  16.  
  17.     global $c;
  18.     $c = new Core();
  19.  
  20.     include('plugin.php');
  21.     $p = new Plugin();
  22.     echo $p->not_work_connection();
  23.     echo $p->work_connection();
  24. ?>
  25.        
  26. <?php
  27.     class Plugin
  28.     {
  29.         function not_work_connection()
  30.         {
  31.             $sql = 'SELECT * FROM `net_country` LIMIT 0 , 1';
  32.             $result = mysql_query($sql);
  33.             while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  34.             {
  35.                 return print_r($row, 1);
  36.             }
  37.         }
  38.  
  39.         function work_connection()
  40.         {
  41.         global $c;
  42.             $result =$c->query('SELECT * FROM `net_country` LIMIT 0 , 1');
  43.             while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  44.             {
  45.                 return print_r($row, 1);
  46.             }
  47.         }
  48.     }
  49. ?>
  50.        
  51. <?php
  52. class Core
  53. {
  54.     private $db;
  55.     function connect()
  56.     {
  57.         $this->db = @mysql_connect($host, $user, $pass);
  58.         @mysql_select_db($base, $db);
  59.  
  60.         //dummy
  61.         @mysql_connect();
  62.     }
  63.  
  64.     function query($sql)
  65.     {
  66.         //notice second param
  67.         return mysql_query($sql, $this->db);
  68.     }
  69. }
  70.  
  71. global $c;
  72. $c = new Core();
  73.  
  74. include('plugin.php');
  75. $p = new Plugin();
  76. echo $p->not_work_connection(); //doing a mysql_query will use the dummy resource and fail
  77. echo $p->work_connection();
  78. ?>
  79.        
  80. include_once("class/sql.php")
  81. $db = new DB();
  82. include_once("class/table.php")
  83. $table = new TableClass();
  84.  
  85. $result =$db->query($table->load(parameters));
  86.         while($row = mysql_fetch_array($result))
  87.         {
  88.             return print_r($row, 1);
  89.         }