0

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

Edited by pritaeas: Removed bbcode

6
Contributors
21
Replies
90
Views
4 Years
Discussion Span
Last Post by spluskhan
Featured Replies
  • 2

    > 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%'") Read More

  • 1

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

  • 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? Read More

  • 1
    IIM 163   4 Years Ago

    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](http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html) … Read More

  • 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'] = … Read More

2

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%'")
0

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

and when i type laptop or other product i get iphone

1

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?

0

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>"; 
    ?>
1

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.

Edited by IIM

0

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

1

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\']."%'") 

`

Edited by IIM

0

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

1

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\']%'") 

Edited by IIM

1

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.

0

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

Edited by diafol: fixed code format

0
$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

Edited by diafol: fixed code formatting

2

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);
?>

Edited by pixelsoul

1

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.

1

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%'")
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.