0

我有以下代码 -

<?php # search.inc
error_reporting(E_ALL); // 
ini_set('display_errors', '1');
include "connect_to_mysql.php";
?>
<?php # search.inc.php

//header file:
include_once ('./includes/header.inc');
?>
<?php

$terms= $_GET['terms'];
echo "You've searched for: " . $terms;

$sql= "SELECT * FROM products WHERE product_name ='Big Tree'"; 
//-run  the query against the mysql query function 
$result=mysql_query($sql);

while ($row = mysql_fetch_array($result)) { // While there are products that match the search criteria, assign their data to a variable and echo their details on a webpage


             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $details = $row["details"];
             $category = $row["category"];
                echo '<br/> <img name="image" src="inventory_images/' . $row['id'] . '.jpg" width="100" height="150"/>';
                echo '<br/>';
                echo '<br/> Product Name: '.$row['product_name'];
                echo '<br/>';
                echo '<br/> <a href="modules/productsdetails.inc.php?id=' . $id . '">Click Here To View Product Page</a>';
                echo '<br/>';
                echo '<br/>';

        }
mysql_close(); //closes the MySQL connection.
?>
<?php
// Include footer file 
include_once ('./includes/footer.inc');
?>

如您所见 - 我已经对其进行了硬编码以搜索“大树”。我的问题是 - 我如何编码以搜索用户输入。

我试过 %$terms% 但没有打印任何结果。

4

4 回答 4

1

您必须在查询中连接要搜索的变量

$sql= "SELECT * FROM products WHERE product_name ='".$terms."'"; 

或者

$sql= "SELECT * FROM products WHERE product_name LIKE '%".$terms."%'"; 
于 2013-03-14T02:43:32.330 回答
1

用这个:

$sql = "SELECT * FROM products WHERE product_name ='$terms'";

无需串联。

于 2013-03-14T02:52:57.083 回答
0

If you're using the innodb engine on mysql 5.5 or below you'll have to use 'LIKE' like so:

$sql= "SELECT * FROM products WHERE product_name like '%".$terms."%'"; 

If you're on mysql 5.6 or you're using a myisam table, you can use MATCH AGAINST if you have a fulltext index on the column you're searching.

$sql= "SELECT match(product_name) against('".$terms."') as relevance 
FROM products 
WHERE relevance>0 
ORDER BY relevance desc"; 

Please look into parameterized queries or at the very least use mysql_real_escape_string to sanitize your variables.

于 2013-03-14T02:50:20.347 回答
0

我为 mysql 创建了一个函数,它使用百分比排序解析字符串。如果你喜欢,你可以使用它。

它几乎可以找到您搜索的所有组合。

安装

首先你需要在mysql中创建一个函数。这是代码。我在phpmyadmin中做到了

DELIMITER //

CREATE FUNCTION search ( search VARCHAR(255), string VARCHAR(255) )
RETURNS FLOAT
BEGIN

   DECLARE searchlength INT;
   DECLARE charcount INT DEFAULT 1;
   DECLARE ch VARCHAR(10); 

   DECLARE a INT DEFAULT 0; 
   DECLARE b INT DEFAULT 1;
   DECLARE c INT DEFAULT 0; 

   SET searchlength = LENGTH(search);

   WHILE charcount <= searchlength DO 
    SET ch = SUBSTRING(search,charcount,1); 

    IF LOCATE(ch,string) > 0 THEN 
      IF LOCATE(ch, string) - 1 = b THEN 
        SET c = c + 1; 
      END IF; 
      SET b = LOCATE(ch, string, b);
      SET a = a + 1; 
    END IF;

    SET charcount = charcount + 1;

   END WHILE;

   IF searchlength = a THEN 
    RETURN (c/30)*100; 
   END IF; 
   RETURN -1; 

END; //

DELIMITER ;

现在,当您像这样从 php 进行 mysql 调用时,您可以使用此功能。

$searchneedle = "Some string you like to search for"

SELECT name,username 
FROM users 
WHERE search(LOWER('$searchneedle'),CONCAT(LOWER(test_cullum))) > -1
ORDER BY search(LOWER('$searchneedle'),CONCAT(LOWER(test_cullum))) DESC
于 2016-09-10T19:40:09.237 回答