在null上调用成员函数query()
问题描述:
我是PHP编程的新手,我正在制作一个接收查询的PHP类,但是当召唤给我时,出现错误"PHP致命错误:在null上调用成员函数查询()".你能帮我看看发生了什么吗?
I am new to programming in PHP and I'm making a PHP class that receives queries but when summoned gives me the error "PHP Fatal error: Call to a member function query () on null". Can you help me to see what's happening?
这是PHP类代码
class conectar{
public $conexion;
var $svrName;
var $user;
var $pwd;
var $dbName;
var $consult;
function set_conexion($new_conexion){
$this->conexion=$new_conexion;
}
function get_conexion(){
return $this->conexion;
}
function set_server($new_svrName){
$this->svrName=$new_svrName;
}
function get_server(){
return $this->svrName;
}
function set_user($new_usere){
$this->user=$new_user;
}
function get_user(){
return $this->user;
}
function set_pwd($new_pwd){
$this->pwd=$new_pwd;
}
function get_pwd(){
return $this->pwd;
}
function set_dbName($new_dbName){
$this->pwd=$new_dbName;
}
function get_dbName(){
return $this->dbName;
}
function conectarDB($svrName, $user, $pwd,$dbName){
$conexion = mysqli_connect($svrName, $user, $pwd,$dbName);
return $conexion;
}
function consultaDB($consult){
$result = $conexion->query($conexion,$consult);
return $result;
}
function disconnectDB($conexion){
$close = mysqli_close($conexion);
return $close;
}
}
所以我打电话给
<?php
include("conectar.php");
$con = new conectar();
$con->conectarDB("localhost","root","root","contactos");
$query = "Select * from pais order by nombre";
$ejeConsulta = $con->consultaDB($query);
while ($result = $ejeConsulta->fetch_assoc()){
echo "<option value='".$result["pais"]."'>".$result["pais"]."</`enter code here`option>";
}
?>
感谢帮助
答
您的变量范围错误.试试这个:
Your variable scopes are wrong. Try this:
function conectarDB($svrName, $user, $pwd,$dbName){
$this->conexion = mysqli_connect($svrName, $user, $pwd,$dbName);
return $this->conexion;
}
function consultaDB($consult){
$result = $this->conexion->query($consult);
return $result;
}