Fetching information from the string of tags

The code retrieves user input and searches a database field called tags.

Within the tags field, there is data such as hello, hey, how, happy, hell stored in a single cell or string.

I attempted the query below, but it only seems to work for "hello" and not "how", "happy", or "hell".

$sql ="SELECT * FROM tws where tags='".$name."' or tags LIKE '".$name.",%' or tags LIKE ',%".$name.",%' or  tags LIKE '%,".$name."'";

Note: I extensively searched Google and Stack Overflow before seeking help here, but unfortunately couldn't get it to function properly.

Answer №1

To retrieve the desired data, execute the query below:

$sql = "SELECT * FROM `records` WHERE `keywords` REGEXP (".$search.")";

Answer №2

There's a small error with one of the percent signs in your SQL query when using LIKE. Remember that MySQL is very strict about spacing, so make sure to include spaces between your tags in the SQL:

$sql ="SELECT * FROM tws where tags='".$name."' or tags LIKE '".$name.",%' 
       or tags LIKE '%, ".$name.",%' or  tags LIKE '%, ".$name."'";

This should fix the issue (just remove the space after "how" for consistent data formatting).

PS: Don't forget to use mysqli_real_escape_string to escape the value of $name.

You might want to consider creating an additional table for your tags, like tws_tags, with fields tws_id and tag. Then you can use a JOIN query to improve efficiency.

Answer №3

To enhance your query, it is advisable to exclude commas in the following manner:

$sql ="SELECT * FROM `tws` WHERE `tags` RLIKE '%[[:<:]]$name[[:>:]]%'";

The symbols [[:<:]] and [[:>:]] have specific significance in MySQL as they denote word boundaries. Additionally, you can directly include variables within double quotes without concatenation ("blablabla $smth txt" instead of "blablabla " . $smth . " txt")

Answer №4

Try using the code snippet provided below:

$query = "SELECT * FROM database WHERE column='".$value."' or 
           column LIKE '".$value."%' or column LIKE
             '%".$value."%' or  column LIKE '%".$value."'";

If the value is '123', your query will appear as shown here:

 $query = "SELECT * FROM database WHERE column='123' or column LIKE '123,%' or column LIKE
 ',123,%' or  column LIKE '%,123'";

This allows for searching not just for '123', but also for ',123'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to constructing this Laravel Eloquent Query

I'm trying to create a nested array using Laravel Eloquent from a basic table structure, but I'm having trouble figuring it out. Here's an overview of my table: https://i.stack.imgur.com/mH5vA.png and here is the desired array format I am ...

Tips for organizing the output of wp_query

I'm looking to organize the results of my wp_query, wanting to arrange them by different parameters without needing to run the query again. Here is what I have so far: $the_query = new WP_Query( $args ); I’d like to sort $the_query; WP_Query gives ...

Deleting a zip file using PHP: A step-by-step guide

After downloading the 17111611185.zip file in my project using ajax, I need to remove it from my Linux server. To achieve this, after redirecting with window.location=data, another ajax call is made to pass 17111611185.zip back to the server. $.ajax({ ...

Encountering the "SQLSTATE[HY000] [2002] php_network_getaddresses" exception during a Laravel migration process

While executing the php artisan migrate command, an error occurs: Illuminate\Database\QueryException SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: select * from information_ ...

Unable to access information through ajax when connecting to mysql database

Having a code that can add, edit, delete and view is good. When all the codes are put together in one file, it works fine. However, wanting to have it separately poses a problem with the "View" part. Despite trying to search for a solution, the functionali ...

I am seeking clarity on the functionality of this PHP code

Recently, I encountered a strange issue on my website using the Joomla template Liberty. Mysterious links would appear on the pages, but would disappear upon clearing the cache, only to resurface later. After thorough investigation, I discovered that the i ...

Converting HTML code to JSON using PHP scripting

Would appreciate your help in resolving a small issue. Although I came across this post, I am still encountering some errors: How can I convert HTML to JSON using PHP? I have created a PHP file that extracts a post from WordPress with the following forma ...

Add a fresh key to a pre-existing array within an object

I have a JSON array that I need to modify by adding a new key. Here is the current structure: stdClass Object ( [set] => Array ( [0] => stdClass Object ( [name] => agenda ...

Guide on transforming Json information into the preferred layout and iterating through the loop

Currently, I am diving deep into the world of JSON and feeling a bit puzzled by data formats, arrays, objects, and strings. First things first, I'm in need of data structured like this (on a jQuery page). Would this be considered an object or an arra ...

Attempting to implement bootstrap pagination using PHP mysqli

Hi there, I'm currently working on implementing a pagination feature using Bootstrap. My goal is to display all results from mySQL data while limiting the data to 10 entries per page. I would appreciate any assistance with this. Here's the code I ...

Using AJAX to dynamically serialize data from a form and POST it in PHP

I have a script on jQuery and HTML5 that dynamically generates a form. Next, I utilize the following code: var OForm = $('#OForm'); // Find disabled inputs, and remove the "disabled" attribute var disabled = OForm.find(':input:disabled&ap ...

When sending an AJAX request to a URL, can I verify in the PHP page whether it is a request or if the page has been accessed?

In order to enhance security measures, I need to prevent users from accessing or interacting with the php pages that will be utilized for ajax functionality. Is there a method available to determine if a page has been accessed through an ajax request or b ...

PHP will send back a response code of 500, but only if it is submitted

Hey there! I've been working on implementing a two-step authentication system on a website, and I'm encountering an issue with an AJAX post. Every time I try to make the AJAX post, it returns a 500 Error. Oddly enough, when I directly run the co ...

Retrieving State Information Using PHP based on City

Is there a way to determine the state based on the city, similar to how we can find city, address, or state based on ZIP code or latitude/longitude? Is there an API that provides this functionality? I am specifically looking for a way to retrieve the stat ...

Generating massive spreadsheets using code

We are currently utilizing OpenPyxl to transfer data from MySQL to Microsoft Excel in XSLX format Nevertheless, we have encountered a challenge due to the large volume of data we are working with. Our system is running out of memory, especially since our ...

Difficulty in packaging JavaScript resources within Laravel Project

Having trouble setting JS functions as global in my project: In the 'resources\js"', I have: numerosALetras.js: /////////////////////////// function unidades_nal(n){ ... } function decenas_nal(n){ ... } function centenas_nal(n){ ...

PHP for Calculating Time to Percentage

I have a question that may seem simple, but I'm unsure how to convert the time difference between two dates into a percentage. Currently, I am utilizing a JQuery plugin available at: The specific script on the page responsible for calculating the pe ...

Utilize the translator equipped with FileType functionality

I need help with adding an upload field of type FileType to a custom form using the FormBuilderInterface. Whenever I attempt to upload a file that exceeds the php upload_max_filesize, I receive the default error message. The link to the code causing this ...

Using Express.js to import a SQL file

I am facing challenges while attempting to import an sql file into Express JS. I have tried various codes involving the mssql and fs modules, but none seem to be working as expected. fs.readFile(__dirname +'/database.sql', function (err, sqlFile ...

Is the user consistently experiencing redirection to a failure page with passport?

I've been struggling with the user login redirection issue on my website. No matter what changes I make, it keeps redirecting to failure instead of success. I'm not sure if I missed something or did something wrong. The documentation for passport ...