Php 致命错误:未捕获错误:在中对null调用成员函数query()

Php 致命错误:未捕获错误:在中对null调用成员函数query(),php,fatal-error,Php,Fatal Error,我运行的代码中有一个问题。它抛出以下错误,如下所示: 致命错误:未捕获错误:调用null上的成员函数查询 在/home/nissah5/sjs.vedantabgp.com/includes/functions.php:25堆栈中 跟踪:0/home/nissah5/sjs.vedantabgp.com/includes/batch.php6: searchstudent 1/home/nissah5/sjs.vedantabgp.com/index.php8: 包括抛出的“/home/niss

我运行的代码中有一个问题。它抛出以下错误,如下所示:

致命错误:未捕获错误:调用null上的成员函数查询 在/home/nissah5/sjs.vedantabgp.com/includes/functions.php:25堆栈中 跟踪:0/home/nissah5/sjs.vedantabgp.com/includes/batch.php6: searchstudent 1/home/nissah5/sjs.vedantabgp.com/index.php8: 包括抛出的“/home/nissah5/s..2{main}” /第25行的home/nissah5/sjs.vedantabgp.com/includes/functions.php

我写的代码在这里。 第25行是指$query=$db->query$query

function searchstudent(){
connectdb();
$condition='1'; 
if(@$_GET['y']=='xii') $condition.=" and xii='".$_GET['yr']."'";
if(@$_GET['y']=='x') $condition.=" and x='".$_GET['yr']."'";
if(@$_GET['v']=='chapter') $condition.=" and chapter='".$_GET['chapter']."'";
$query="select name, x, xii, description, id from alumni where ".$condition;
echo $query;
$query=$db->query($query);
$row1=$query->fetchAll();
$query->closeCursor();
$i=1;
foreach ($row1 as $row){ $i=($i+1)%2; if($i==0){?>
    <div class="row">
        <div class="col-sm-5">
            <img class="img-responsive" src="images/templatemo_image_4.jpg" alt="image 4" />    
        </div>
<?php searchrow($row);} else{ ?>
        <div class="col-sm-1"></div>
        <div class="col-sm-5">
            <img class="img-responsive" src="images/templatemo_image_5.jpg" alt="image 5" />
        </div>
<?php searchrow($row);?>
    </div>
<?
}
}
if ($i==0) echo "</div>";

不确定代码中的问题来自何处。。但有两种方法可以解决这个问题:

你不能用WHERE和。你需要这样做: 其中1=1,且。。你的情况。 尝试将查询更改为:

$query=选择校友的姓名、x、xii、说明、id,其中1=1.$condition

您未连接到数据库,$db变量为空

确保您的连接数据库;初始化$db,因为它实际上为空


!!!谢谢,这就是问题所在。我如何检查我的$db是否为空?检查更新的答案。您可以使用var_dump$db查看它的购买情况,看看它是否有任何功能。谢谢。问题解决了。
select name, x, xii, description, id from alumni where 1