Jump to content

Recommended Posts

Hi all,

 

I have a mysql statement as follows:

 

SELECT id, name
FROM customer
WHERE customer_type IN ('J', 'L')

 

The variables in the brackets (i.e J, L, etc) can be passed in through a php function. Note that these variables are strings, and not integers (and have to remain as such).

 

My problem is in trying to escape the input from the php function, and I land up with something like:

 

SELECT id, name
FROM customer
WHERE customer_type IN \'J\', \'L\')

 

Which of course mysql doesnt like.

 

Any ideas on the best way to get around this?

Link to comment
https://forums.phpfreaks.com/topic/190097-escaping-text/
Share on other sites

Sorry, I obviously didnt explain myself properly - I AM using mysql_real_escape_string on each variable, and thats what results in the query looking as follows:

SELECT id, name
FROM customer
WHERE customer_type IN (\'J\', \'L\')

 

But mysql doesnt like that query - the escapes cause it to throw an error.

Link to comment
https://forums.phpfreaks.com/topic/190097-escaping-text/#findComment-1002994
Share on other sites

Can you show us how exactly you create this query?

Sure, the simplified version is something like:

 

function get_customer($customer_type=null)
{
  $sql = "SELECT id, name
  FROM customer";
  $sql .= ($customer_type != null) ? " WHERE customer_type IN (" . mysql_real_escape_string($customer_type) . ")" : '';
    
  //query database and return result, etc
}

 

The customer_type field in the customer table is a char(1) field. The values are not stored as (for example) 'J', but just as J (i.e. without inverted commas).

 

The query doesnt work if its something like the following:

SELECT id, name
FROM customer
WHERE customer_type IN (J)

 

It does work if its like this:

SELECT id, name
FROM customer
WHERE customer_type IN ('J')

 

I need to escape the input, but as soon as I do that, it fails (as described previously)

 

Link to comment
https://forums.phpfreaks.com/topic/190097-escaping-text/#findComment-1003078
Share on other sites

Hi

 

Appears all you need is:-

 

function get_customer($customer_type=null)

{

  $sql = "SELECT id, name

  FROM customer";

  $sql .= ($customer_type != null) ? " WHERE customer_type IN ('" . mysql_real_escape_string($customer_type) . "')" : '';

   

  //query database and return result, etc

}

 

You need to escape input so that any characters in the field that are meaningful to SQL are converted to something equivalent that isn't meaningful to MySQL. This isn't really anything to do with surrounding a character value with inverted commas.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/190097-escaping-text/#findComment-1003084
Share on other sites

Is $customer_type always a single value, or can it be a comma delimited string? If it's the latter you need to explode it first, escape each value, put each value in single quotes, and implode it again.

 

If it's alway a single value, it should look like this:

 

function get_customer($customer_type=null)
{
  $sql = "SELECT id, name
  FROM customer";
  $sql .= ($customer_type != null) ? " WHERE customer_type = '" . mysql_real_escape_string($customer_type) . "'" : '';
    
  //query database and return result, etc
}

(note additional single quotes)

Link to comment
https://forums.phpfreaks.com/topic/190097-escaping-text/#findComment-1003085
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.