Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:28:40+00:00 2026-06-18T07:28:40+00:00

I am using a var inside a sql statement, which checks on a field

  • 0

I am using a var inside a sql statement, which checks on a field which could have many tags in eg

adtags="home, about, article"

$queryVar = 'home'



$bannerName = $wpdb->get_results($wpdb->prepare("select  pic.filename
,       pic.pid
,       fv1.field_value as MovieName
,       fv2.field_value as Adlink
from    ngg_pictures pic
inner join 
    nggcf_fields f1
on      f1.field_name = 'adtags'
inner join 
    nggcf_field_values as fv1
on      fv1.pid = pic.pid
    and fv1.fid = f1.id
inner join 
    nggcf_fields f2
on      f2.field_name = 'adlink'
inner join 
    nggcf_field_values as fv2
on      fv2.pid = pic.pid
    and fv2.fid = f2.id
where   fv1.field_value like '%s'   ",$queryVar));

note (fv1.field_value is pulling adtags)

I can get this to work where I can find one instance if adtags only contains one tag, and i removed the queryvar methid – but what I would like to do is search fv1.field_value for the occurance of $queryVar (home or anything thats passed)

I think in order to do this I need to use a wildcard, such as %home%

where   fv1.field_value like '%home%'   

but how do I achieve this when the queryvar is referenced as %s, can I escape this out somehow ?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:28:42+00:00Added an answer on June 18, 2026 at 7:28 am

    Answer 1

    Would help to see the whole line of code.
    Just put the variable in the string.

    "where fv1.field_value like '%$queryVar%'";
    

    Before that you could do something to avoid possible query injection.

    Take only the first word.

    $queryVar = preg_replace('/\s.*$/', '', $queryVar);
    

    Take only letters.

    $queryVar = preg_replace('/[^a-z]+/i', '', $queryVar);
    

    Answer 2

    Do this before the query:

    $queryVar = "%$queryVar%";
    

    Answer 3

    Escape with %.

    where   fv1.field_value like '%%%s%%'   ",$queryVar));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have try { using (var eventWaitHandle = EventWaitHandle.OpenExisting(name)) { eventWaitHandle.Set(); } Environment.Exit(0); }
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
I have a long running SQL query inside a page that I've sped up
I have a Linq to SQL class. There is a one to many relationship
I have two databases with the same schema inside a Sql 2008 R2 Server,
Im using a select sql statement PDO fetchData, $user = 'admin'; sql = 'SELECT
using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
using (var client = new WebClient()) { html = client.DownloadString(some_string); //do something html =
using (var openFileDialog1 = new OpenFileDialog()) { openFileDialog1.Reset(); if (!string.IsNullOrEmpty(ExcelFilePath)) { string fileName =
using (var dataContext = new realtydbEntities()) { var user = ( from aspnet_Roles rol

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.