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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:47:26+00:00 2026-06-02T22:47:26+00:00

I have a couple of basic questions on parametrized queries Consider this code: $id

  • 0

I have a couple of basic questions on parametrized queries

Consider this code:

$id = (int)$_GET['id'];
mysql_query("UPDATE table SET field=1 WHERE id=".$id);

Now the same thing using a parametrized query

$sql = "UPDATE table SET field=1 WHERE id=?";
$q = $db->prepare($sql); 
$q->execute(array($_GET['id']));

My questions are:

  1. is there any situation where the first code (i.e. with the (int) cast) is unsafe?
  2. is the second piece of code OK or should I also cast $_GET['id'] to int?
  3. is there any known vulnerability of the second piece of code? That is, is there any way an SQL attack can be made if I am using the second query?
  • 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-02T22:47:28+00:00Added an answer on June 2, 2026 at 10:47 pm
    1. is there any situation where the first code (i.e. with the (int) cast) is unsafe?

      I’m not a PHP expert, but I think there shouldn’t be. That’s not to say that PHP doesn’t have bugs (either known or yet to be discovered) that could be exploited here.

    2. is the second piece of code OK or should I also cast $_GET[‘id’] to int?

      Likewise, the second piece of code should be absolutely fine – even if the data type was a string, MySQL would know not to evaluate it for SQL as it’s a parameter and therefore only to be treated as a literal value. However, there’s certainly no harm in also performing the cast (which would avoid any flaws in MySQL’s handling of parameters) – I’d recommend doing both.

      EDIT – @Tomalak makes a very good point about cast potentially resulting in incorrect data and suggests first verifying your inputs with sanity checks such as is_numeric(); I agree wholeheartedly.

    3. is there any known vulnerability of the second piece of code? That is, is there any way an SQL attack can be made if I am using the second query?

      Not to my knowledge.

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

Sidebar

Related Questions

I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
There have been a couple of questions very close to this topic, but none
I found this code in a couple of websites (http://goo.gl/usUSP): <?php // Basic request
Say I have a couple of basic objects like so: [Serializable] public class Base
I am a very new programmer, I have made a couple basic applications, however
I have a fairly basic TCP server keeping track of a couple connections and
I have couple questions regarding some C++ rules. Why am I able to call
You'll have to forgive me if this is a really basic question; I haven't
Firstly, I have found a couple of related questions on the issue with links
I see there are a couple of similar questions, but this is a bit

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.