开发者

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜