0

I'm facing a problem which I don't know how to solve. Let's start explaining:

I've got the following class:

class MyClass{

    function MyClass($mysql_dbcon){
        $this->mysql_dbcon = $mysql_dbcon;
    }

    function execute(){
        include("myscript.php");
    }

}

myscript.php is an HTML template which uses Ajax to comunicate with other PHP scripts. Since these other PHP scripts aren't included directly in the class I'm not able to get the variable mysql_dbcon and use a different connection for each instance. Let's guess I've got the following instances:

$i1 = new Myclass($dbcon1);
$i1->execute();

$i2 = new Myclass($dbcon2);
$i2->execute();

Both instances will execute an Ajax function which call other processing scripts but these scripts can't access the instance's mysql_dbcon variable due to they're independent script executions.

How can I solve this? Is it correct to store the MySQL connection in $_SESSION so all the scripts can access it? Maybe this:

$_SESSION['instance_identifier'] = $this->mysql_dbcon;

What I want to achieve

I want to create a class in which the developer can set a MySQL connection and each instance can access to different databases so each instance will show data from different databases. The problem is the dynamic loading of that data.

Thanks in advance

ProtectedVoid
  • 1,293
  • 3
  • 17
  • 42
  • 3
    You should move logic from "myscript.php" into class method, it makes no sense to have a method which includes another file with additional logic. Since myscript.php requires mysql connection it is safe to assume that it is not a html template but in fact a script which incorporates db logic. – Vladimir Cvetic Sep 27 '16 at 09:22
  • What are you trying to achieve here (the bigger picture)? Why do you want to manage the db connections? Can't each app do it's job? – ka_lin Sep 27 '16 at 09:25
  • 1
    @Nolwennig yes, it's the "java style" notation for the constructor in php, but __construct works the same – d3cima Sep 27 '16 at 09:28
  • `function MyClass($mysql_dbcon)` seems be `function __construct($mysql_dbcon)` ? (yes, but since PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor.) Thanks @d3cima – Nolwennig Sep 27 '16 at 09:33

2 Answers2

3

You cannot solve it.

but this script can't access the instance's mysql_dbcon variable due to it's an independent script execution

Exactly. Period. You're starting an entirely different PHP script which shares absolutely nothing with the previous script. In fact, by the time the AJAX request is initiated by the browser, the "existing" MySQL connection will already be torn down and closed since the entire PHP script has already stopped.

"myscript.php" will simply have to open a new connection. You cannot serialise a connection into a session; it's not data which can be represented in a serialised form. This is exactly the same mechanism as any other two independent PHP files, the fact that include and AJAX are involved changes nothing.

Also see https://stackoverflow.com/a/13840431/476

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
1

Is it correct to store the MySQL connection in $_SESSION so all the scripts can access it?

No.

I'm facing a problem

You probably aren't, or at least it's not the problem you think it is. It's likely there is no reason for you to need to try to "share" the same database connection across the scripts, even if you could. If you provide more context about why you want to access the same connection, then further advice can be given. There is probably a better way to achieve what you want.

Perhaps you are trying to access one query's result set in different places, in which case the answer would not be trying to share a connection but probably to pass the result set itself.


Additional note about your syntax

class MyClass {

    function MyClass($mysql_dbcon) {

Looks like you're using outdated PHP 4 syntax for your constructors. See the manual entry on constructors in PHP 5 for the modern standard.

It's also good practice to define the visibility of your class members using the public/protected/private keywords. See the manual entry on visibility.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191