English 中文(简体)
如何限制从其他类别进入神秘面纱线?
原标题:How to restrict access to mysql connection from other classes?
  • 时间:2012-01-13 22:34:43
  •  标签:
  • php
  • mysql

Sup! We have core class with mysql connection, than we include plugin and we need that plugin cant access to our DB without core class methods.

index.php

<?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();
?>

plugin.php

<?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); 
            }
        }
    }
?>

我需要限制使用包括文字的出入,但假日可以使用核心方法进行查询。 如何做到这一点?

最佳回答

如果没有第二段,我sql_query就使用了我sql_link所用的最后链条,这样你就可以在真实的电离层后形成一种 du:

<?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();
?>
问题回答

为每个类别创造在数据库中做某些工作的部门,与不同的用户建立单独的链接,并在Plugin类别中保持这一联系,并利用数据库图书馆开展这项工作。 我认为这是一个好的解决办法。

当然,你允许用户使用。 (每组或组别的不同用户)

不清楚这是否有助于,但我通常做的是三类 s连接,而另一类是具体表格,一是操纵(假装、装载、删除功能)。

之后,在我的索引或任何网页上,我做了以下工作:

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); 
        }

something like that is just brief but hope it helps.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签