2016-12-25 41 views
-1

我運行的代碼存在問題。它引發這個錯誤,這裏給出:致命錯誤:未捕獲錯誤:調用成員函數在null中的查詢()

Fatal error: Uncaught Error: Call to a member function query() on null in /home/nissah5/sjs.vedantabgp.com/includes/functions.php:25 Stack trace: #0 /home/nissah5/sjs.vedantabgp.com/includes/batch.php(6): searchstudent() #1 /home/nissah5/sjs.vedantabgp.com/index.php(8): include('/home/nissah5/s...') #2 {main} thrown in /home/nissah5/sjs.vedantabgp.com/includes/functions.php on line 25

我寫的代碼是這裏。 第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>"; 

}

回聲給出了這樣的結果

select name, x, xii, description, id from alumni where 1 
+0

https://secure.php.net/variables.scope !!! –

+0

謝謝你的問題(y) –

回答

0

不知道您的問題來自於你的代碼..但也有可能解決這個問題,2種選擇:

  1. 您不能使用WHERE and。你需要這樣做: WHERE 1=1 AND .. your condition。 嘗試將您的查詢改爲:

    $ query =「select name,x,xii,description,id from alumni where 1 = 1」。

  2. 您沒有連接到您的數據庫,並且您的$db變量爲空。

    確保您的connectdb();初始化您的$db,因爲它實際上是null

+0

如何檢查我的$ db是否爲空? –

+0

檢查更新後的答案。你可以使用'var_dump($ db)'來檢查它的購買情況,看看它是否有任何東西.. – TheUnreal

+0

THanks。問題解決了。 –

相關問題