class DB_conn{
function getConnected($servername,$useranem,$pass,$db) {
$mysqli = new mysqli($servername, $useranem, $pass, $db);
if($mysqli->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $mysqli;
}
这是我的连接字符串函数,我正在使用mysqli
。我必须为mysqli
查询使用两个参数。如何在下一个函数中调用$mysqli
?当我在$mysqli
函数中使用fetch_login
时,它给出了一个未定义变量的错误。
注意:未定义的属性:DB_conn :: $ mysqli in 第44行的D:\ xampp \ htdocs \ mysqli \ connect.php
public function fetch_login(){
//$mysqli;
$sql = mysqli_query($mysqli,"select * from login");
return $sql;
}
答案 0 :(得分:-1)
尝试在变量和函数之前使用$ this关键字来访问类成员
class DB_conn
{
private $mysqli;
public function getConnected($servername,$useranem,$pass,$db)
{
$mysqli = new mysqli($servername, $useranem, $pass, $db);
if($mysqli->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $mysqli;
}
public function fetch_login()
{
//$mysqli;
$sql = mysqli_query($this->$mysqli,"select * from login");
return $sql;
}
}