Jump to content

apostrophe in query problem


New Coder

Recommended Posts

Hello All,

 

I am trying to execute a query on my page that retrieves all a members details.

 

$conn = odbc_connect('DBname,'username','password');

      if(!$conn) {exit("Err:Conn"); }

 

$sql = "select * from members where member_id = '$member_id' ";

 

$rs = odbc_exec($conn, $sql);

if( !$rs )

{

exit ("Could not execute Query");

}

 

The member ID is created from their surname and a unique number

eg. Member: Joe Bloggs, Has Member ID: BLO987654

 

this will allow the query to excecute fine, but.. If I have a member Joe O'Rielly their Id number becomes O'R876543 and when thats put into the variable $member_id the query becomes

 

$sql = "select * from members where member_id = 'O'R876543' ";

 

The qeuery is then treating the apostrophe in the ID number as the end of the string and doesn't know what the rest of it is, so it just gives a could not execute query error.

 

How can I get it to recognise the apostrophe in the ID number as just part of the string??

 

Many Thanks

 

Link to comment
Share on other sites

You can escape the quote using a preceding \

 

so the following code should work

$sql = "select * from members where member_id = '".str_replace("'","\'",$member_id)."'";

 

\ is used as the escape character in MySQL but I believe it should work in most other SQL variants

 

 

 

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.