-2

please why do i receive this error. when i try to make a new search, that's the error i get. below is my code. what did i do wrong?

    <?php
    define('DB_HOST','localhost');
    define('DB_USER','user');
    define('DB_PASS','pass');
    define('DB_NAME','dbname');

    $conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
    mysql_select_db(dbname) or die(mysql_error());
    $output = '';
    if(isset($_POST['search'])) {
        $searchq = $_POST['search'];
        $searchq = preg_replace("#[^0-9a-z]#i", "",$searchq);
        $query = "SELECT * FROM track WHERE keywords LIKE '%$searchq%'";
        $count = mysql_num_rows($query);

        if($count == 0) {
            $output = 'There was no search results!';
        } 
        else {
            while($row = mysql_fetch_array($query)) {
            $consignee = $row['consignee'];
            $desti = $row['desti'];
            $date = $row['date'];

            $output =  "<table class=\"zebra\"><thead><tr><th>Consignee</th><th>Destinations / Remarks</th><th>Date</th></tr></thead><tfoot><tr><td>$consignee</td><td>$desti</td><td>$date</td></tr></tfoot></table>";

        }
    }


    }
?>
gen_Eric
  • 214,658
  • 40
  • 293
  • 332
KofYeb
  • 15
  • 3

1 Answers1

0

You need something like:

$query = mysql_query("SELECT * FROM track WHERE keywords LIKE '%$searchq%'");
$count = mysql_num_rows($query);

Edit: and you also need to take a look at How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
MillaresRoo
  • 3,717
  • 1
  • 31
  • 36
  • it worked but my if($count == 0) { $output = 'There was no search results!'; } is not working, when i click just the submit button nothing happen it just gives me a results from the db. – KofYeb Dec 16 '13 at 20:11
  • It's not working because it always returns results? Maybe you need to refine your query. – MillaresRoo Dec 16 '13 at 20:27
  • how do i do that? please! – KofYeb Dec 16 '13 at 20:43
  • Well, I don't know what you want to do but, e.g., if you submit an empty 'search' parameter, your query will look like: `"SELECT * FROM track WHERE keywords LIKE '%%'"` and this select returns all rows from your table. – MillaresRoo Dec 16 '13 at 20:54
  • ok i will try that. thanks – KofYeb Dec 16 '13 at 21:25