-1

There is a problem in the code that i run. It throws this error whihc is given here:

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

The code that I have written is Here. The line no 25 refers to $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>";

}

Echo gives this result

select name, x, xii, description, id from alumni where 1
Nishant Sah
  • 141
  • 3
  • 13

1 Answers1

0

Not sure where your problem comes from in your code.. but there are 2 options that may solve this issue:

  1. You can't use WHERE and. You need to do it like that: WHERE 1=1 AND .. your condition. Try change your query to:

    $query="select name, x, xii, description, id from alumni where 1=1 ".$condition;

  2. You are not connected to your database and your $db variable is empty.

    Make sure your connectdb(); initializes your $db because it's actually null.

TheUnreal
  • 23,434
  • 46
  • 157
  • 277