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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:07:03+00:00 2026-05-27T23:07:03+00:00

I’m using mysqli extension in php for connection to database. I’ve such a simple

  • 0

I’m using mysqli extension in php for connection to database. I’ve such a simple question. Is it better to use mysqli instead of mysql and why is it necessary to use mysqli_real_escape_string ? what is this function doing exactly ? 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-05-27T23:07:04+00:00Added an answer on May 27, 2026 at 11:07 pm

    I’ll put a little example not using SQL. Imagine you have this PHP code:

    <?php
    echo 'Hello, world!';
    

    Now you want to replace world with O'Hara:

    <?php
    echo 'Hello, O'Hara!'; // Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'
    

    Yeah, of course, that is not valid PHP. You need to escape the single quote since it’s interpreted as a literal quote rather than the string delimiter:

    <?php
    echo 'Hello, O\'Hara!';
    

    You have exactly the same problem when composing SQL queries. If you inject random input into your code, sooner or later it’ll break. You need to encode input so it’s handled as literal input rather than broken code.

    How can you do that? Well, MySQL accepts \' just like PHP (though it’s only a coincidence: other database engines use other escape methods). So the dumbest solution is to add back slashes here and here:

    SELECT id FROM user WHERE name='O\'Hara';
    

    Of course, it’s a lot of work to hard-code all the possible characters that need escaping (and you’ll probably forget some of them) so you can use a function that does the job for you: either mysql_real_escape_string() or mysqli_real_escape_string().

    The question is: is this good enough? Well, it kind of works, but it leads to annoying code that’s difficult to maintain:

    $sql = "UPDATE user SET name='" . mysql_real_escape_string($name) . "' WHERE id='" . mysql_real_escape_string($id) . "'";
    

    … and you still need to take care of surrounding the complete value with single quotes… which are not always mandatory (think of numbers)… What a mess. Can’t someone invent something better? Good news is: they did! It’s called prepared statements:

    // Just an example, I invented the syntax
    $sql = 'UPDATE user SET name=:name WHERE id=:id';
    $params = array(
        'name' => "O'Brian",
        'id' => 31416,
    );
    $MyDbConnection->execute($sql, $params);
    

    In real life:

    • MySQLi has the prepare() method to accomplish this. Find some examples there.
    • Legacy MySQL extension… has nothing: it does not support prepared statements at all! If you use this extension, you are stuck with the annoying add-quotes-yourself and string concatenation methods.

    I hope this explains the whole question.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is

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.