简体   繁体   English

表格搜索的正确语法(mysql)(php)

[英]correct syntax for a table search (mysql)(php)

I was wondering what the correct syntax using mysql_query would be to search a table for things that match the 'key'. 我想知道使用mysql_query的正确语法是在表中搜索与“键”匹配的东西。

For example, say I want to search for a client. 例如,说我要搜索客户。 and assume this is a non-user input. 并假定这是非用户输入。 IE run from a script. IE从脚本运行。

$client_search = mysql_query("SELECT * FROM `clients` WHERE `client_name LIKE '%".$_POST['key']."%' OR `crmurl` LIKE '%".$_POST['key']."%' ORDER BY `client_name`") or die(mysql_error());
$fetch_client = mysql_fetch_array($client_search);

if I run something like this I get told the syntax is incorrect, more specifically the error is.. 如果我运行这样的命令,我会被告知语法不正确,更具体地说是错误。

 `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'crmurl` LIKE '%Fake%' ORDER BY `client_name`' at line 1` 

however I can't spot anything amiss, is there anyone with a sharp-eye out there that could assist. 但是我找不到任何不对劲的地方,有没有人能提供帮助。

NOTE: Please I really dont want 5+ people telling me how mysql is obsolete and it should be using PDO::, I know but its a quick script that takes no user input and has sanitized table data. 注意:请我真的不希望有5个以上的人告诉我mysql是如何过时的,它应该使用PDO :::,我知道,但是它是一个无需用户输入即可清理表数据的快速脚本。

As you are enclosing the whole query within double quotes, there is no need of appending the sql string because within double quotes all PHP variables will get replaced with the corresponding values. 在将整个查询括在双引号中时,无需附加sql字符串,因为在双引号中所有PHP变量都将替换为相应的值。 Also you missed one backticks in fieldname client_name . 您也错过了字段名client_name一个反引号。

$client_search = mysql_query("SELECT * FROM `clients` WHERE `client_name LIKE '%".$_POST['key']."%' OR `crmurl` LIKE '%".$_POST['key']."%' ORDER BY `client_name`") or die(mysql_error());
                                                          missing there ^         

Change the query to 将查询更改为

$client_search = mysql_query("SELECT * FROM `clients` WHERE `client_name` LIKE '%$_POST['key']%' OR `crmurl` LIKE '%$_POST['key']%' ORDER BY `client_name`") or die(mysql_error());

Try like this: 尝试这样:

$client_search = mysql_query("SELECT * FROM `clients` WHERE `client_name LIKE '%$_POST[key]%' OR `crmurl` LIKE '%$_POST[key]%' ORDER BY `client_name`") or die(mysql_error());
$fetch_client = mysql_fetch_array($client_search);

Change SQL Query to 将SQL查询更改为

SELECT * FROM `clients` WHERE `client_name` LIKE '%$_POST[key]%' OR `crmurl` LIKE '%$_POST[key]%' ORDER BY `client_name`"

or 要么

SELECT * FROM `clients` WHERE client_name LIKE '%$_POST[key]%' OR crmurl LIKE '%$_POST[key]%' ORDER BY client_name"

Modify your query like this 像这样修改您的查询

"SELECT * FROM `clients` WHERE `client_name` LIKE '%".$_POST['key']."%' OR `crmurl` LIKE '%".$_POST['key']."%' ORDER BY `client_name`"

There's a backtick missing for client_name client_name缺少引号

You are missing ` in query on column name 您在查询列名称时缺少`

Select * from `clients` WHERE `client_name` ...

So query should be like - 所以查询应该像-

$client_search = mysql_query("SELECT * FROM `clients` WHERE `client_name` LIKE '%".$_POST['key']."%' OR `crmurl` LIKE '%".$_POST['key']."%' ORDER BY `client_name`") or die(mysql_error());

You are missing closing "`" at the table name. 您缺少在表名处关闭“`”的情况。 Change it to 更改为

 WHERE `client_name`

Then final query will be like 然后最终查询将像

$client_search = mysql_query("SELECT * FROM `clients` WHERE `client_name` LIKE '%".$_POST['key']."%' OR `crmurl` LIKE '%".$_POST['key']."%' ORDER BY `client_name`") or die(mysql_error());

You have an error in your SQL syntax 您的SQL语法有误

$client_search = mysql_query("SELECT * FROM `clients` WHERE `client_name LIKE '%".$_POST['key']."%' OR `crmurl` LIKE '%".$_POST['key']."%' ORDER BY `client_name`") or die(mysql_error());

Replace 更换

`client_name `client_name

with

client_name

So your query will be like 所以你的查询就像

$client_search = mysql_query("SELECT * FROM `clients` WHERE `client_name` LIKE '%".$_POST['key']."%' OR `crmurl` LIKE '%".$_POST['key']."%' ORDER BY `client_name`") or die(mysql_error());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM