Is it possible to use a $myqli->real_escape_string in side of a custom class with out loading the connection again?
Is it possible to use a $myqli->real_escape_string in side of a cust开发者_如何学运维om class with out loading the connection again? Take the code below. $mysqli is created twice is it possible to use the connection already established?
<?php
$mysqli = new mysqli('127.0.0.1','user','password','table');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit('connect failed!');
}
// connected
class save {
public $datatosave = '';
function __construct ($new) {
$mysqli = new mysqli('127.0.0.1','user','password','table');
$this->datatosave = $mysqli->real_escape_string($new);
}
}
$infromation = " ' test";
$newinfo = new save ($infromation);
echo $newinfo->datatosave;
$mysqli->close();
?>\
should still output \' test
Since you are catching the connection with the $mysqli variable, just pass that into your __construct function when you initialize the class.
class save {
public $datatosave = '';
function __construct ($new, $mysqli_attr) {
$this->datatosave = $mysqli_attr->real_escape_string($new);
}
}
$infromation = " ' test";
$newinfo = new save ($infromation, $mysqli);
echo $newinfo->datatosave;
精彩评论