简体   繁体   中英

PHP 7 : Using a database connection handle within and outside a class

Admittedly, I am not an expert programmer. Please be tolerant if I use the wrong terminology. I need to migrate PHP code from PHP 4 to 7. I diligently added 'i' to all the database calls (eg myslq_connect became mysqli_connect). In the code below, the connection is done in the class constructor (it wasn't used in the PHP 4 compatible code).

class SQL_Query
{

    var $sql;
    var $link; 

    public function __construct()
    {
           $link = mysqli_connect('localhost','dbUser','dbPass', 'dbDB');
           if (mysqli_connect_errno())
           {
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
           }
    }

public function SQL_Query()
{
    self::__construct();
}

/* Functions to structure the SQL query here have been remove to keep this post compact */

function Perform_Query($link,$return_object = true)
{
    $this->sql = $this->Get_Query();
    /* The following connection code works 
    $link = mysqli_connect('localhost','dbUser','dbPass', 'dbDB');
    if (mysqli_connect_errno())
    {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          return false;
    }*/
    $result = mysqli_query($link,$this->sql);

Unfortunately, the $link doesn't seem to be available in the method Perform_Query. The code above doesn't work. I fixed it by moving the connection code inside the method Perform_Query (as commented out in the code above). The following code in the main PHP file then starts to work:

        $query = new Select_Query();
        $query->Add_Order('total_points');               
        $query->Add_Field('username');
        $query->Add_Field('total_points');
        $query->Add_Condition(1, 'validated');
        $query->Add_Table('t_player');
        $room_players = $query->Perform_Query();

Note that there is a class Select_Query that constructs the SQL query text. It starts as follows:

class Select_Query extends SQL_Query

However, the call to mysqli_connect is used elsewhere in the code, and I want to be able to make the following code work as well:

          $query = new Select_Query();
          $query->Add_Field('t_player_id');
          $query->Add_Table('t_player');
          $query->Add_Condition($owner, 'username');
          $qd = mysqli_query($link,$query->Get_Query());
          if(!$qd)
             echo("Error description: " . mysqli_error($link));
          $result = mysqli_fetch_array($qd); 

For this to work, I need to have the handle to the database connection available outside of the class, so I can use it in the mysqli_query call (or so I think). The code uses database interactions a lot. If there is no other choice, I could change all of it such that there is no mysqli call outside the SQL_Query class, but if there is a way to avoid it, I would be really grateful to whomever points me in this direction.

Unfortunately, the $link doesn't seem to be available in the method Perform_Query.

The $link variable you're using in Perform_Query() method is a local variable , not the instance variable . And look at the constructor method of the class, you need to assign the connection handler to the instance variable $this->link , not the local variable $link . Your constructor and Perform_Query() methods would be like this:

public function __construct(){
       $this->link = mysqli_connect('localhost','dbUser','dbPass', 'dbDB');
       // your code
}

and

function Perform_Query($return_object = true){
    // your code
    $result = mysqli_query($this->link,$this->sql);

For this to work, I need to have the handle to the database connection available outside of the class, ...

Make the instance variable $link public , so that the connection handler could be accessible from outside of the class.

class SQL_Query{
    public $link; 
    ...

and later you can access the connection handler from outside of the class to execute a query.

$query = new Select_Query();
$query->Add_Field('t_player_id');
$query->Add_Table('t_player');
$query->Add_Condition($owner, 'username');
$qd = mysqli_query($query->link,$query->Get_Query());
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM