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 6981095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:06:44+00:00 2026-05-27T18:06:44+00:00

Ok, so I am fairly new to php and mysql. I am creating a

  • 0

Ok, so I am fairly new to php and mysql. I am creating a php front end to mysql database. I am using PDO driver to access the database, since it prevents sql injection attacks. So far it has been fine until I came to this problem. I have a search function where a user can type company name full or partial to search for data about it.

Here is PDO statement I am using to carry out the search in database:

SELECT CompName FROM CompanyName 
WHERE CompName REGEXP :name 
ORDER BY CompName ASC LIMIT 1

So then I can prepare, bind what user types in search field to parameter name and execute the statement. As long as user does not type any metacharacters it works. Here is the very basic regular expression I insert into the PDO statement instead of name:
^whatusertyped -since originally I am looking for a complete match. Since some company names do contain periods and such I want to be able user to type those characters and my regular expression to take them as literals as opposed to metacharacters. So far this is how I have been replacing metacharacters to get their literal meaning:

user types: C. to look for company name that starts with C.

php function inserts ^ and \\\\ to get an output of ^C\\.

mysql gets: ^C\\. and searches for C. in the beginning of company name where period is literal and not meant to say “look for wild card character”.

So I am trying for C. because there is a company in the database whose name starts like that and I should get a match, but I don’t, the PDO statement returns back an empty array.

I did try the query:

SELECT CompName FROM CompanyName 
WHERE CompName REGEXP "^C\\\\." 
ORDER BY CompName ASC LIMIT 1

directly in mysql and got back a match and I also directly executed the query in PHP without prepare, bind and got back a match. The problem seems to me is in preparing and binding the search term to a parameter, but I have not been able to figure out what it is exactly and how to solve it. I really want to use bind and prepare like I said to avoid sql injection.

Any help would be greatly appreciated. Hopefully I explained the problem clearly.

  • 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-27T18:06:44+00:00Added an answer on May 27, 2026 at 6:06 pm

    I believe the value you bind to :name should be just ^C\., not ^C\\.. You don’t need to do any escaping for the sql level, just for the regex level (i.e., \. for a literal .).

    Also, consider using WHERE CompName LIKE 'C.%'. You don’t need a regex here. You would use like so:

    function like_escape($s) {
    // Do like-expression escaping
        return addcslashes($s, '%_');
    }
    $searchprefix = 'C.';
    $sql = 'SELECT CompName FROM CompanyName WHERE CompName LIKE ? ORDER BY CompName ASC';
    $stmt = $db->prepare($sql);
    $likeclause = like_escape($searchprefix).'%';
    $db->bindValue(1, $likeclause, PDO::PARAM_STR);
    $stmt->execute();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, so I'm fairly new to PHP and SQL/MySQL so any help is appreciated.
I am fairly new to PHP, as I mostly skilled in the front end
I am implementing a fairly simple calendar on a website using PHP and MySQL.
I'm fairly new to php and mysql and I'm on the home stretch of
I am fairly new to PHP and mysql and was wondering if I could
I am fairly new to php and mysql and am having an issue with
I'm fairly new to PHP and have built a medium sized website using standard
I'm fairly new to PHP and MySQL (experienced with other languages). Basically I want
I'm fairly new to php and I'm using a script that creates a function
To put it simply i am a fairly new PHP coder and i was

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.