Table display error

Hello All,

I have made a page with a search bar. It searches for keywords from three columns in a table. Now the problem I am facing is, I want the table to appear after I search for a keyword. But what is happening is, the header of the table, is coming before the submission itself. And along with it, I get a notice and warning.

Notice: Undefined variable: result in C:\xampp\htdocs\sdis\data\search-2d-data.php on line 35
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\sdis\data\search-2d-data.php on line 35

Here is my code, can someone please help me.


<?php
  include $_SERVER["DOCUMENT_ROOT"].'/sdis/core/init.php';
  include $_SERVER["DOCUMENT_ROOT"].'/sdis/includes/overall/header.php';
  if(isset($_POST['search'])===true){
  	$search=$_POST['search'];
$query = "SELECT * FROM `2d_raw_data` WHERE `basin` LIKE '%".$search."%' OR `area` LIKE '%".$search."%' OR `block` LIKE '%".$search."%'";
  $result=mysql_query($query);
}
?>

<form id="form1" name="form1" method="post" action="">
<h2><center>Search 2D Data</center></h2>
<input type="text" class="form-control" name="search" required/><br/>
<center><button class="btn btn-lg btn-primary" type="submit" value="Search">Search</button></center><br/>
</form><br/><br/>


<table width="100%" border="1" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="100%" border="1" cellspacing="1" cellpadding="3">
<tr>
<td colspan="50"><strong><center>2D Data</center></strong> </td>
</tr>

<tr>
<td align="center"><strong>Basin</strong></td>
<td align="center"><strong>Area</strong></td>
<td align="center"><strong>Block</strong></td>


</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['basin']; ?></td>
<td><?php echo $rows['area']; ?></td>
<td><?php echo $rows['block']; ?></td>
</tr>

<?php
}
?>

</table>
</td>
</tr>
</table>

<?php include $_SERVER["DOCUMENT_ROOT"].'/sdis/includes/overall/footer.php'; ?>

I think that means the query is returning zero results.

Also you should not be using the mysql_* functions as they are depreciated. More information http://www.php.net/manual/en/intro.mysql.php

Yes, the query isn’t returning anything useful for some reason.

I’m assuming the connection code is in the included (maybe require would be better here?) “init.php” file?

You could do 2 things that should help you troubleshoot.
You could echo out the query to make sure it looks like what you expect it to.
You can add an “or die mysql_error()” to the line that runs the query.

  • though using mysqli would be better - you should change over now while working on the code IMHO

If you are saying that when the page is initially loaded up the table header is appearing, then var_dump($_POST[‘search’]) and see what is causing that condition to fail.


  if(isset($_POST['search'])===true)

BTW, as that is checking if the anything has been typed in the search box, why not test for something a bit more useful.


  if(strlen($_POST['search']) > 2)

At least here you are getting 3 chars and your search has got a bit of a chance of working …

The error is caused because you have no conditional (if) check where you loop through any potential results - so it is trying to loop through a non-existent result set.