It's okay to make a db conection multiple times? PHP - MySql -
i'm using class called mymysqli conect, make queries, etc.
mymysqli.php:
... public function query($query){ $this->total_queries++; $result= mysqli_query($this->conection, $query); if(!$result){ echo "error: " . mysqli_error($this->conection); exit; } return $result; } ... ... public function free(){ mysqli_free_result($this->result); } ...
and have other class define users. inside it, have public function modify data of user:
public function makeactive($id){ if($this->userexists($id)){ $db = new mymysqli; $db->query("update users set useractive =1 iduser=".$id); $db->free(); return 1; } return 0; }
and function userexists //checks if user exists :
public function userexists($id){ $db = new mymysqli; $db->query("select iduser users iduser=".$id); if($db->num_rows($db->query) === 1){ return 1; } return 0; $db->free(); }
so, can see, i'm calling new mymysqli 2 times in function, it's okay ? or should'nt use function check if user exists, including check makeactive function?
thank !
my solution use mysqli persistant connection.
when creating connection mysql, use following example.
mysqli_connect('p:servername', 'username', 'password', 'database', port);
to open persistent connection must prepend p:
hostname when connecting.
according documentation http://php.net/manual/en/mysqli.persistconns.php
the idea behind persistent connections connection between client process , database can reused client process, rather being created , destroyed multiple times. reduces overhead of creating fresh connections every time 1 required, unused connections cached , ready reused.
Comments
Post a Comment