简体   繁体   中英

How to find a text field is existing in a MYSQL table

I want to store user Search Queries in to MYSQL databse. I have created a Search_Queries field in mysql table and it type is text , Collation is utf8_general_ci .

If the search word is exists in the table, then update the Search_Count++ else insert the search word into the table

I have used for find existing

**SELECT ID FROM search_details WHERE Search_Query='keyword' LIMIT 1**

if it return empty then insert the keyword

**INSERT INTO search_details SET Search_Query='".$SearchQuery."',Search_Count=1"**

else update the search_count field

UPDATE search_details SET Search_Count='".$Search_Count."' WHERE ID='".$ID."'

my problem is if the Search Query is not exists in the table

**SELECT ID FROM search_details WHERE Search_Query='keyword' LIMIT 1**

this returns with last row instead of NULL

successfully insert the Search Query into the table and search count set as to 2

basically , isnt that what you wanted to do in here

INSERT INTO search_details SET Search_Query='".$SearchQuery."',Search_Count=1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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