0

I looked around, but couldn't find an answer to this anywhere.

If I'm creating a class and I need to query MySQL in different functions of the class, is there a way to require a conn.php file only once to be used for all functions?

So far I'm requiring a connection file in each of the functions, but it seems resource expensive to require a connection multiple times throughout a class.

...EDIT ... So like this maybe?

private $conn;
function __construct() {
$this->conn = new mysqli("", "", "", "");
}

function insert() {
    $sql = "";
    $query = $this->conn->prepare($sql);
    $query->bind_param();
    $query->execute();
    $query->free_result();
    $query->close();
}

function update() {
   $sql = "";
   $query = $this->conn->prepare($sql);
   $query->bind_param();
   $query->execute();
   $query->free_result();
   $query->close();
}

This is working really well. Is there any drawback to this? Is there a more professional way to do this?

  • There is a feature with mysql and PHP called persistent mysql connections and depending on whether you use the mysql or mysqli API, the way to execute it is different. Just make sure you close the connection before the PHP program terminates or you'll leave dead connections open and run into an error along the lines of "too many connections" from mysql. – Mike -- No longer here Sep 09 '15 at 18:17
  • If you're in a class, you can have its constructor connect to the database and then save that connection resource in a variable like `$this->db`. – Rocket Hazmat Sep 09 '15 at 18:18

4 Answers4

2

You can set up the connection to MySQL in the constructor of your class and assign the connection to a member variable, like this:

private $oConnection = null;

public function __construct() {        
    $this->oConnection = mysqli_connect("12.34.56.789", "db_username", "db_password");
}

Then you can use that member variable in any other functions of the class. For example:

public getDataResult() {
    $sSql = 'select * from my_table limit 10';
    $oResult = @mysqli_query($this->oConnection, $sSql);
    return $oResult;
}
mynx
  • 68
  • 4
0

Try to stay away from using global variables in your code, especially when they reference potentially dangerous values that you would not want your entire program to have access to, particularly database connections.

That said, you can approach this problem using a simple database class, as @mynx suggested, or go with dependency injection and pass the database variable as an argument to your individual functions, if you wanted to go the procedural approach. Either approach (object oriented or procedural) will help you manage your code much easier than declaring global variables which can cause a whole mess of code if you are not careful.

Since you seem to be going the object oriented direction, you can combine @mynx's answer with the idea of then passing the instantiated database class as a dependency to other classes that may need access to the database, which is a great way of creating maintainable and debuggable code.

Community
  • 1
  • 1
samrap
  • 5,245
  • 5
  • 29
  • 55
0

try this

class mydb{

private $true = false;

function __construct($host,$dbname,$user,$password){
$this->true = mysqli_connect($host,$buser,$password,$dbname);
}


function queries($query){
if($this->true){
if(preg_match('/(UPDATE|DELETE FROM|INSERT INTO|DROP TABLE|CREATE TABLE|ALTER TABLE)/',$query)){
if($rv = mysqli_query($this->true,$query))
return true;

}elseif(preg_match('/(update|delete from|insert into|drop table|create table|alter table)/',$query)){
if($rv = mysqli_query($this->true,$query))
return true;
}else{
if($results = mysqli_query($this->true,$query)){


$result = array();
while($obj = mysqli_fetch_object($results))


$result[] = $obj;
return $result;
}

}

}

}
$mydb = new mydb($database_host,$database_name,$database_user,$database_password);
 call the query
$result = $mydb->queries($sql);

in other function make $mybd to global example

function ,,,,,,.....(){


global $mydb;

}

-1

If your database connection exists in the global scope, then you just need to declare it as global inside each function.

$db = mysqli_connect(...);

function getData(){

    global $db;

    $sql = 'SELECT * FROM table';
    $result = mysqli_query($db, $sql);
    return $result;

}

If your functions are all class-based, which as you mention '__construct()' it sounds like they are, then the answer from @mynx is your solution.

MaggsWeb
  • 2,912
  • 1
  • 11
  • 20