gpt4 book ai didi

php - SQL命令无法获取单词相关句子

转载 作者:行者123 更新时间:2023-11-29 13:55:00 25 4
gpt4 key购买 nike

我想从数据库中获取与给定单词相关的句子。假设这里有一个mysql表。

id-      sentence  
1- This is a web page
2- This is spiderweb
3- Here is a webspider
4- create anyWebPage

这是一个简单的代码。

<?php  
// ignore all syntax error in this code if present.
$input = 'web';
$sql = mysql_query("
SELECT * FROM `table_name`
WHERE sentence LIKE('%$input%')
");
// echo The given result.
?>

如果我使用 LIKE %....% 那么所有结果都会显示。
如果我使用 LIKE ...% 那么它不能给出任何结果,因为没有从 $input = 'web';
开始的句子我想得到那些世界是从web
开始的句子所需结果
1-这是一个网页页面
3 - 这是一个网络蜘蛛

请指导我如何才能得到这样的结果。
谢谢......

最佳答案

Quotes should be around %不在它们内部,例如 LIKE('%$input%')。请注意,无论您使用什么字符串比较,它都取决于是否 collation is case sensitive or insensitive :

The default character set and collation are latin1 and latin1_swedish_ci, so nonbinary string comparisons are case insensitive by default. This means that if you search with col_name LIKE 'a%', you get all column values that start with A or a.

mysql_已弃用,请使用 mysqli_PDO:: .

您没有转义输入 ( SQL Injection )。

您正在声明 $input 并使用 $input

另请检查FULLTEXT search options in MySQL用于速度优化。

或者,如果您需要匹配整个单词,您可以使用 REGEXP像这样:

SELECT * FROM test WHERE sentence REGEXP '([^[:alnum:]]|^)$input([^[:alnum:]]|$)'

Example here 1 其中 [^[:alnum:]] 匹配除数字和字母(逗号、句号、破折号)和 ^/$ 开头之外的所有内容/分别是字符串的结尾。

<子>[1] 只是 Pastebin,sqlfiddle现在已关闭:-/

关于php - SQL命令无法获取单词相关句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16001848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com