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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:30:44+00:00 2026-05-31T22:30:44+00:00

I am using a bit of borrowed code, and am concerned for its vulnerability

  • 0

I am using a bit of borrowed code, and am concerned for its vulnerability to SQL injection…

// Set the default namespace to utf8
$mysqli->query("SET NAMES 'utf8'");
$userID = $_POST['userID'];
$json   = array();
if($result = $mysqli->query("SELECT * FROM towns WHERE userID=".$userID)) {
while ($row=$result->fetch_assoc()) {
    $json[]=array(
        'townID'=>$row['townID'],
        'townName'=>$row['townName']
    );
}
}
$result->close(); 
header("Content-Type: text/json");
echo json_encode(array( 'towns'  =>   $json ));

Question 1: Does $mysqli->query("SET NAMES 'utf8'"); have any form of sql injection assistance?
Question 2: Should I be using some form of real_escape_string?

If I need to use real_escape_string, should I set the var like this:

$userID = $_POST['userID'];
$userID = $mysqli->real_escape_string($userID);

Then, I can use it just like I currently am in my query?

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-31T22:30:45+00:00Added an answer on May 31, 2026 at 10:30 pm

    Question 1: Does $mysqli->query(“SET NAMES ‘utf8′”); have any form of sql injection assistance?

    Worded this way – definitely NO.
    But see the note at the bottom.

    Question 2: Should I be using some form of real_escape_string?

    You are asking out of delusion that escaping has something to do with injections, while it is not. So, let’s rephrase your question:

    Question 2a: Should I be take some precautions against SQL injections?

    Yes.
    You have three choices.

    1. Since $userID being a number, you may cast it to that type explicitly, $userID = intval($_POST['userID'];)
    2. Mysql (when not in the STRICT MODE) let you send the number as a string. Strings has to be formatted according to these 2 simple rules, as it shown in the Rob’s answer:
      • it has to be delimited by single quotes
      • these quotes have to be escaped.
    3. You can use prepared statements. To use such a fashionable way of running sql queries you have to use prepare() + execute() methods instead of query() and bind your variable using bind_param().

    A note regarding the title question.

    is setting namespace to utf-8 any form of sql injection protection?

    Yes, in a way.
    In some, extremely rare circumstances, it is.
    For some rare encodings real_escape_string family function may fail proper delimiter escaping. To avoid that, one have to set up the client encoding using set_charset family function, mysqli->set_charset() in your case.

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

Sidebar

Related Questions

I have some old code written in C for 16-bit using Borland C++ that
A bit of background first: I am using base code from a remote SVN
I am using this bit of jQuery code to get href of the link:
I'm using a bit of jquery code which uses li items to select which
I'm using a bit of jquery to search Flickr, produce a set of thumbnails,
Using a bit of code like this: static bool Intersects(Vector2 a1, Vector2 a2, Vector2
I'm using a bit of code I found on the internet somewhere to compress
I'm using this bit of code: fileBrowser() { String toReturn = null; JFileChooser Chooser
I'm using a bit of code that I found on the web to manufacture
I am using Bit Miracle LibTiff.Net. I cannot find any sample code to take

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.