[clug] PHP gurus? How can I find an "anonymous" mysql resource?

Leigh Purdie intersect at gmail.com
Sat Jun 17 22:11:25 GMT 2006


Heya Tony,

Pretty easy if your DB connection script is object oriented - it's
simple to save off the DB connection to a variable, which can be
reused at a later stage.

Something like this (very simple example):
Class MyDB
{
    public  $database;

     function Open()
    {
         $this->database=@mysql_connect("my.sql.server","root","password");
    }

     function Query($string)
    {
        $resource=mysql_query($string,$this->database);
        return($resource);
    }

    ....

$db=new MyDB;
$result=$db->Query("Select blah from blah");
while($myrow=mysql_fetch_row($result)) {
   ...

However, regardless of whether your script is object oriented or not,
if you can force your db script to give you back this return value
from mysql_connect, you can reuse it as part of your query. ie:
mysql_query($string) becomes mysql_query($string,$connection_handle);

Can you post some sample code? I might be able to explain a bit more
clearly if I can reference your code. (Feel free to email direct, if
you reckon the list will be utterly bored ;)

Regards,

Leigh.

On 6/18/06, Tony and Robyn Lewis <beakysnugger at yahoo.co.uk> wrote:
> Pardon being OT, but I'm hoping someone on list can help with a PHP problem.
>
> We've got lots of hand-crafted PHP pages, and there is typically one DB
> connection per page.  Now we want to insert ("include") a common set of
> pages that, amongst other things, makes a different DB connection.
>
> Unfortunately, once our included code finishes, the existing MySQL
> connection is a goner, because it was never saved (as in, at
> mysql_connect() time), and up to now it worked, because mysql_query()
> will use the resource of the last mysql_connect().
>
> Is there a way I can essentially put the current MySQL resource (the
> variable associated with the connection) onto a stack (or just save it
> in a var), let the inserted code do its bit, and then restore / pop the
> old resource to be the current resource?
>
> Or is there a better way, other than rewriting the existing code to
> properly save and use the resource?
>
> Please, no sermons - this is inherited code, and I dislike PHP enough
> already.  I especially disrelish the idea of hand-editing scores of PHP
> pages looking for mysql calls.
>
> TIA,
>
> Tony
>
> --
> linux mailing list
> linux at lists.samba.org
> https://lists.samba.org/mailman/listinfo/linux
>


More information about the linux mailing list