Reputation: 139
i'm having a bit of an issue trying to use mysqli_connect the same way i use mysql_connect
this would be an example of my code:
QUERY.PHP
class classQuery{
public function __construct(){
require('conex/classConex.php');
require('conex/statement/classStatement.php');
$this->ObjConex = new classConex;
$this->ObjStatement = new classStatement;
}
public function Query($email){
$this->ObjConex->Conex();
$query='SELECT user_email from table where email='.mysql_real_escape_string($email).'';
$consulta = $this->ObjStatement->Select($query);
return $consulta;
}
CLASS STATEMENT
class classStatement{
public function __construct(){
$this->ObjConex = new classConex;
}
public function Select($query){
$query_execute = mysql_query($query);
while($row = mysql_fetch_row($query_execute)){
$consulta=htmlentities($row[0]);
}
return $consulta;
}
}
CLASS CONEX
class classConex{
public function Conex(){
require ('conex.php');
mysql_connect ($server,$dbuser,$dbpasswd) or die('Error de Conexión');
mysql_select_db($dbname);
}
}
Ok, now i want to use mysqli_connect instead of mysql_connect, according to php manual my new connect class should be something like this:
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
now that my connection is an object i won't be able to execute my query from ClassStatement the same way i'm doing it, i have tested returning the object from the connect class but that means more code that i find redundant...is there a more elegant way to do this?
Upvotes: 3
Views: 9205
Reputation: 377
Mysqli_connect is the newer version of mysql library.
Here I in mysqli stands for improved.
Few things have been introduced with Mysqli.
They are,
1.Prepared statements. 2.Object oriented interface. 3.Support for multiple statements. 4.Embedded server support.
Upvotes: -2
Reputation: 2847
How about structuring your class like this:
class Database {
public function __construct ( $server, $dbuser, $dbpasswd, $dbname ) {
$this->dbhandle = new mysqli($server, $dbuser, $dbpasswd, $dbname);
}
public function Select ($query) {
$result = $this->dbhandle->query($query);
while ( $row = $result->fetch_row() ){
// Whatever you're doing here...
//$consulta = htmlentities($row[0]);
}
return $consulta;
}
}
So you could use it with this code:
class Query{
public function __construct(){
require('conex/classDatabase.php');
$this->Database = new Database($host, $user, $pass, $dbname);
}
public function Query ($email) {
$query = "SELECT user_email from table where email='".mysql_real_escape_string($email)."'";
$consulta = $this->Database->Select($query);
return $consulta;
}
}
I've included the object oriented syntax in my examples. Since you're using objects anyway, you'll probably get along with it.
Upvotes: 2
Reputation: 191749
mysql_connect
creates a global connection that you are depending on. This is already inelegant. You will have a lot more control (especially if you need to maintain multiple connections simultaneously) if you treat the connection as an object instance -- which is what mysqli
forces you to do.
It's not redundant either .. it's just clear.
Upvotes: 3