在null上调用成员函数query() [英] Call to a member function query() on null

查看:207
本文介绍了在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;     
}

这篇关于在null上调用成员函数query()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆