suppose i have 10 products in my database
Example:
iphone 2 - iphone 3g - laptop i3 - laptop i7 - nokia n96 etc

when i enter iphone in my search i see all records stored in database thats my prob!

what i want ?
suppose if i enter laptop i7 or laptop then i get result for laptop i7 or laptop

here is my code..

<?php
// Connects to your Database
mysql_connect("host.com", "userr", "pass") or die(mysql_error());
mysql_select_db("products") or die(mysql_error());
$data = mysql_query("SELECT * FROM Products")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>new:</th> <td>".$info['new_price'] . "</td> ";
Print "<th>old:</th> <td>".$info['old_proce'] . "</td> ";
Print "<th>faulty:</th> <td>".$info['Faulty_price'] . " </td></tr>";
}
Print "</table>";
?>

if any one have better code then please give me tankxx

SELECT * FROM Products

This always selects all products. You should add a WHERE clause, something like:

$search = 'iphone';
$data = mysql_query("SELECT * FROM Products WHERE name LIKE '%$search%'")

i try it but ther is another prob i get only iphone details

and when i type laptop or other product i get iphone

Of course, you should change $search to use what you have in your form.

i have mamy many products

pritaeas is saying that instead of $search = 'iphone'; you should be using the result of your search form on the right side of the =. It depends on the code for your search form. What is the name or ID of your textbox?

let me simple evry thing ...

database (Products):
id
name
new_price
old_price
faulty_price

This is my Search code:

<form method="get" action="db1.php">
<label><font size="5">Search For: </label>
<input type="text" name="query" /> 
<input type="submit" name="submit" value="Lookup" />
<input type="reset" value="Reset" </form>

And Here Is My db1.php code:

<?php // Connects to your Database 
    mysql_connect("host.com", "userr", "pass") or die(mysql_error());
    mysql_select_db("products") or die(mysql_error());
    $data = mysql_query("SELECT * FROM Products") 
    or die(mysql_error()); 
    Print "<table border cellpadding=3>"; 
    while($info = mysql_fetch_array( $data ))
    { 
    Print "<tr>"; 
    Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
    Print "<th>new:</th> <td>".$info['new_price'] . "</td> ";
    Print "<th>old:</th> <td>".$info['old_price'] . "</td> ";
    Print "<th>faulty:</th> <td>".$info['Faulty_price'] . " </td></tr>"; 
    } 
    Print "</table>"; 
    ?>

Instead of

$data = mysql_query("SELECT * FROM Products")

Use

  $data = mysql_query("SELECT * FROM Products where name LIKE '%$_GET['query']%'") 

Here $_GET['query'] is the value passed from the form and on the basis of value passed ,it will search.
So it will search in table Products containing $_GET['query'].

Read this for more search criterias.

i change it & got this error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/a3434099/public_html/db1.php on line 218

forgot to escape '.

  $data = mysql_query("SELECT * FROM Products where name LIKE '%$_GET[\'query\']%'") 

or use this

`

 $data = mysql_query("SELECT * FROM Products where name LIKE '%".$_GET[\'query\']."%'") 

`

replace this
$data = mysql_query("SELECT * FROM Products where name LIKE '".%$_GET[\'query\']%."'")

And Result:
Parse error: syntax error, unexpected '%' in /home/a3434099/public_html/db1.php on line 218

Again missed %.Try now

$data = mysql_query("SELECT * FROM Products where name LIKE '%".$_GET[\'query\']."%'") 

or try

  $data = mysql_query("SELECT * FROM Products where name LIKE '%$_GET[\'query\']%'") 
Member Avatar for diafol

Perhpas escaping the get var would be appropriate e.g. with mysql_real_escape_string:

$q = mysql_real_escape_string($_GET['query']);
$data = mysql_query("SELECT * FROM Products where name LIKE '%$q%'"); 

BUT, mysql is set for deprecation, using mysqli or PDO should afford a greater longevity.

Error Again

1:

$data = mysql_query("SELECT * FROM Products where name LIKE '%".$_GET[\'query\']."%'")

PHP Error Message

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/a3434099/public_html/db1.php on line 218

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ']' in /home/a3434099/public_html/db1.php on line 220

2:

$data = mysql_query("SELECT * FROM Products where name LIKE '%$_GET[\'query\']%'")

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/a3434099/public_html/db1.php on line 218
$q = mysql_real_escape_string($_GET['query']);
$data = mysql_query("SELECT * FROM Products where name LIKE '%$q%'"); 

got this

Parse error: syntax error, unexpected T_LOGICAL_OR in /home/a3434099/public_html/db1.php on line 220

Give this a try and see if it works

<?php
    // Connects to mysql
    $link = mysql_connect("host.com", "userr", "pass");
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }

    // Connect to database
    $db = mysql_select_db("products") or die(mysql_error());
    if (!$db) {
        die('Could not connect: ' . mysql_error());
    }

    $_GET['query'] = "%". $searchQuery . "%";
    $searchQuery = strip_tags($searchQuery);
    $searchQuery = trim($searchQuery);


    $data = mysql_query("SELECT * FROM Products where name LIKE '$searchQuery'") or die("MySQL Error: " . mysql_error());

    // get number of results
    $numRows=mysql_num_rows($data); 

     if ($numRows == 0) 
     { 
        echo "Sorry, we could not find an entry to match your query"; 
     }else{

    Print "<table border cellpadding=3>";
    while($info = mysql_fetch_array( $data, MYSQL_ASSOC))
    {
        Print "<tr>"; 
        Print "<th>Name:</th> <td>". $info['name'] . "</td> ";
        Print "<th>new:</th> <td>". $info['new_price'] . "</td> ";
        Print "<th>old:</th> <td>". $info['old_price'] . "</td> ";
        Print "<th>faulty:</th> <td>". $info['Faulty_price'] . " </td></tr>"; 
    } 
    Print "</table>";
    }

    // free up the memory from the query
    mysql_free_result($data);
    // close the mysql connection
    mysql_close($link);
?>
Member Avatar for diafol

Parse error: syntax error, unexpected T_LOGICAL_OR in /home/a3434099/public_html/db1.php on line 220

This is upstream from the SQL query from the looks of it.

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ']' in /home/a3434099/public_html/db1.php on line 220

Remove "\".I have checked this and its working.

$data = mysql_query("SELECT * FROM Products where name LIKE '%".$_GET['query']."%'")

This is working fine.I have checked this.I am not getting T_LOGICAL_OR .Might bee somewhere you might have missed punctuations .

$q = mysql_real_escape_string($_GET['query']);
$data = mysql_query("SELECT * FROM Products where name LIKE '%$q%'")

i tanks all you to give me your important time & specially i tanks to Mr.IIM & Mr.diafol

My Prob Solved By: Mr.IIM

if it's working than why downvoted?

commented: tankx bro +0

oh sorry bro i new in this forum i corrected.

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.