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

  • SEARCH
  • Home
  • 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 6737283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:13:51+00:00 2026-05-26T11:13:51+00:00

I am making a Doctrine query and I have to do a wildcard match

  • 0

I am making a Doctrine query and I have to do a wildcard match in the where clause. How should I escape the variable that I want to insert?

The query I want to get:

SELECT u.* FROM User as u WHERE name LIKE %var%

The php code until now:

   $query = Doctrine_Query::create()
                ->from('User u')
                ->where();

What should come in the where clause? The variable I want to match is $name

  • 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-05-26T11:13:51+00:00Added an answer on May 26, 2026 at 11:13 am

    Nobody answered your question correctly, so I’ll make a stab at it.

    ->where('u.name LIKE ?', array("%$name%"));
    ->where('u.username LIKE ?', '%'.$username.'%')
    

    Neither of these are safe. Let me explain a few scenarios.

    Scenario 1

    Imagine you want to let users search for matching usernames, but you never want to list ALL usernames. Perhaps you don’t want somebody to easily steal a list of a million usernames from you. somewhere prior to this code, you did something like this:

    if (strlen(trim($name)) < 5) throw Boogey_Monster_Exception();
    

    You thought this would prevent somebody from leaving the field blank and pulling down a list of all usernames… but in reality the user can submit “_____” or “%%%%%” or anything similar to get a list of all usernames, not just matching 5 or more known characters.

    I have personally seen this form of attack used on several large, public websites.

    Scenario 2

    You have a website with lots of users and lots of user data. You have 10,000,000 rows in your user table. You want to enable site’s users to find another user’s username by searching for known prefixes.

    So you write some code like this, modified slightly from the example above to only have a wildcard AFTER the search string.

    ->where('u.name LIKE ?', array("$name%"));
    

    If you have an index on u.name, then this LIKE query will use the index. So if the user submits $name=”john”, then this query will efficiently match users like johndoe, johnwayne, johnwaynegacy, etc.

    However, if the user submits $name=”%john” instead, this query no longer uses the index and now requires a full table scan. On a very large database this can be a very slow query.

    The MySQL manual on SQLi mentions this same thing (pages 78-79) and I googled for some examples of slow query performance and found one link.

    This may not sound like a big deal, but for sites backed by an RDBMS, the RDBMS is usually a significant bottleneck, and much of the performance engineering revolves around reducing contention on the RDBMS. IF you have a handful of users launching an attack that ties up a database handle for 60+ seconds, and you have a small pool of database handles, you can see how this could quickly scale to monopolize all of your database handles and prevent legitimate users from being able to get one.

    Links

    http://dev.mysql.com/tech-resources/articles/guide-to-php-security-ch3.pdf

    http://forums.mysql.com/read.php?24,13397,13397

    Solution

    Anyway, the better solution (as mentioned in the MySQL manual linked above and by commenter @Maxence, is to use addcslashes()):

    $username = addcslashes("%something_", "%_");
    

    Note that since the sql examples here use prepared statements, which are completely immune to sql injection, it is not necessary or desirable to use mysql_real_escape_string(); the escaping it performs is solely to prevent sql injection. What we’re trying to prevent is wildcard injection, and that requires a function that escapes the two sql wildcard characters, ‘%’ and ‘_’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a doctrine model that has a method getSomethingId() and I'm making something
Making game of life I need to a have a grid that is 30x20
Making a ship game because I am incredibly original.. With that aside, I have
Making a painting app using HTML5 and Canvas. I think I want to have
I'm having a hard time making sense of the Doctrine manual's explanation of cascade
Making a word document of our network set-up. We have about 7 servers and
Making an adobe flex ui in which data that is calculated must use proprietary
Making UML sequence diagram in VS 2010RC I've observed that there is no activation
Making websites that appear correctly in IE is a big problem. Is there any
I have a bit of a question that has been bothering me for a

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.