Hi, this script wont show the result from my data base and i dont know why it might be here

if ($x==1)
    $construct .= " description LIKE '%$search_each%'"; 
    else
    $construct .= " AND description LIKE '%$search_each%'";

i tried to change the description to other to another column like location but no luck.

<?php

include ('connect.php');

$button = $_GET['submit'];
$search = $_GET['search'];

if (!button)
  
  echo "you didnt submit a keyword."; 

else

{

if (strlen($search)<=2)

   echo "search term to short.";
 else 
{
  echo " You searched for <b>$search</b><hr size='1'>";

  //connect to our database

 $search_exploded = explode(" ",$search);

 foreach($search_exploded as $search_each)

{

// construct query

$x++;
if ($x==1)
    $construct .= " description LIKE '%$search_each%'"; 
    else
    $construct .= " AND description LIKE '%$search_each%'";
    
      }
   // echo out construct
  
 $construct = "SELECT * FROM flats WHERE $construct";
 $run = mysql_query($construct);

 $foundnum = mysql_num_rows($run);

if (foundnum==0)
  echo "No results found.";
else

{
   echo "$foundnum result found!<p>";

 while ($runrows = mysql_fetch_assoc($run))

{

// get data

   $select = $runrows['type'];
   $title = $runrows['title'];
   $location = $runrows['location'];


echo "

    echo $title;
    echo <br>;
    echo $select;
    echo <br>;
    echo $rent;
    echo <br>;
    echo $location;
	echo <hr>";

}     


      }
    }
  }


?>

A good start is to turn on error reporting:

error_reporting(E_ALL);
ini_set('display_errors', '1');

Show errors in your mysql query:

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

echo the final query formed and then you will know whether it is failing or some variable is not set

Hi,

i did the error reporting, thanks for that.

It says that i have some undefined variables in line 33, 35 and 47

line 33 is the $x
line 35 is the $construct
line 47 is the $foundnum

I dont really know what to do with them i tried to google it but had no luck. any ideas to how i can solve this error?

<?php
       include ('connect.php');
       $button = $_GET['submit'];
        $search = $_GET['search'];
        if (!button)
{
       echo "you didnt submit a keyword.";
  }  else
       {
       if (strlen($search)<=2)
      echo "search term to short.";
        else
      {
        echo " You searched for <b>$search</b><hr size='1'>";
         //connect to our database
       $search_exploded = explode(" ",$search);
$x=0;
        foreach($search_exploded as $search_each)
        {
        // construct query
        $x++;
       if ($x==1)
      $construct .= " description LIKE '%$search_each%'  AND";
        else
        $construct .= "  description LIKE '%$search_each%'  AND";
       }
$construct=substr($construct,0,-4);
       // echo out construct
       $construct1 = "SELECT * FROM flats WHERE $construct";
       $run = mysql_query($construct1);
       $foundnum = mysql_num_rows($run);
        if (foundnum==0)
        echo "No results found.";
        else
         {
       echo "$foundnum result found!<p>";
       while ($runrows = mysql_fetch_assoc($run))
        {
       // get data
        $select = $runrows['type'];
      $title = $runrows['title'];
      $location = $runrows['location'];
        echo ".$title."<br>".$select."<br>".$rent."<br>".$location"<hr>";
        }
       }
       }
        }
      ?>

hi , try like this, it will help you .

i found some errors in ur code and fixed them. it may be working fine.

Hi,

i did the error reporting, thanks for that.

It says that i have some undefined variables in line 33, 35 and 47

line 33 is the $x
line 35 is the $construct
line 47 is the $foundnum

I dont really know what to do with them i tried to google it but had no luck. any ideas to how i can solve this error?

Undefined variables are variables on which you are doing operations on, but have yet to define.

For example:

$a = $x +1;

The undefined variable there would be $x. The program has no way to know what value $x is, since it has not been defined before.

To fix it:

$x = 1;
$a = $x +1;

Now $a = 2 correctly. Since we have a value for $x.

The reason PHP still carries on is that it is dynamically typed language and very lenient. When a value is not defined, it will convert it to a NULL for you, and then if you do an operation on it, such as addition in the example, it will convert it to 0 in order to do the addition.

Note: It will still give you a warning however, since it is bad practice to not define variables before using them.

$a = $x +1;

PHP will thus give $a the value 1, even though $x is undefined.

Oops posted wrong solution, sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.