Jump to content

mysql_real_escape_string()


brendan6

Recommended Posts

Does:

 

$id = mysql_real_escape_string($_GET["id"]);

$result = mysql_query("SELECT * FROM table WHERE id = '$id'");

 

Have the same affect as:

 

$id = $_GET["id"];

$query = sprintf("SELECT * FROM table WHERE id = '%s'",mysql_real_escape_string($id));

$result = mysql_query($query);

 

?

Link to comment
Share on other sites

that isn't very safe, you should always test if the data type off a users input is valid, like run a is_numeric on it for example, and mysql_real_escape_strign

 

It looks like the "id" column in this case is probably an auto increment numeric data type, and hence would need to be validated to see if it's a valid integer. This is best done with ctype_digit() and not is_numeric() since that function allows periods and negative signs (dash) like in -1.2.

 

<?php

$nbr = 12.34;

if (is_numeric($nbr))
    echo 'The value of $nbr is numeric';  // Displays this!
else 
    echo 'The value of $nbr is not numeric';

if (ctype_digit($nbr))
    echo 'The value of $nbr is an integer';
else 
    echo 'The value of $nbr is not an integer';  // Displays this!
   
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.