0

I have defined my connection in my method. However, I want to be able to create an instance of this connection for use on a login page.

Normally the way I do this on pages such has insert, select etc.

$con = new dbclass(); //class where my methods are housed
$con-> createcon(); //create con has the connection handler, ($con=mysqli_connect(x.x.x.x)

Fair enough, I use this a lot. However, I'm not having any luck with this procedure on my login page. It appears inadequate. Errors returned are :

    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, object given in C:\xampp\htdocs\a\login.php on line 103

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, object given in C:\xampp\htdocs\a\login.php on line 104

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in C:\xampp\htdocs\a\login.php on line 109

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\a\login.php on line 111

This is an example piece of code that returns an error :

$user = mysqli_real_escape_string($con, $user);

To date I have only used real_escape_string on an update method within a class. I managed to satisfy the connection handler requirement on real_escape_string by doing this

$esc_id = mysqli_real_escape_string($this->conn, $id);

So my question is, do I somehow need to create an instance on my login page. And if so, how?

4

2 回答 2

0
$user = mysqli_real_escape_string($con, $user);

In this code you use variable class wrongfully.replace with this code:

$user = mysqli_real_escape_string($con->$con, $user);

Variable $con in the class should be public.

于 2013-07-30T22:41:27.060 回答
0

$con = new dbclass();

But you've not shown us the source for this class.

$con-> createcon(); //create con has the connection handler, ($con=mysqli_connect(x.x.x.x)

The comment doesn't make any sense. Where do you put the return value from mysqli_connect() if it's in a local variable then it's no longer accessible after the method returns. It needs to be stored somewhere so you can pass it to mysqli_functions(), e.g.

class dbclass {
 var $dbhandle;
 function createcon() {
   $this->dbhandle=mysqli_connect(...);
 }
}

$con=new dbclass();
$con->createcon();
// then you can do....
$user = mysqli_real_escape_string($con->dbhandle, $user);
于 2013-07-30T22:46:59.040 回答