Jump to content

Problem with quotes?


Mr Chris

Recommended Posts

Hello People,

 

I am trying to grab the contents of a tab delimited file and put it in a MYSQL table.  Now it's working fine apart from one thing.

 

Say I have the tab delimited file i'm grabbing the contents of like so:

 

Timothy O'Brien

 

foreach ( $data as $line ) {
    $val = trim ($line); 
    $arr = explode ("\t", $val); 
    $val_new = cleanQuotes(implode ("','", $arr));
    $query = "INSERT INTO parse_original (player1,player2) VALUES ('".$val_new."')";  
mysql_query($query) or die(mysql_error()); 
} 

 

Now the above is grabbing each of the contents like so 'Timothy', 'O'Brien' (With Quotes)

 

and with the above the query will fail becauseof the 'O'Brien'  So I have created a function called cleanQuotes that takes any quotes and converts it into into a html version (')

 

But it's now this doing this:

 

'O'Brien'

 

Can anyone help me keep the function i've written but only run that on the text within the quotes so it parses O'Brien as ?

 

'O'Brien'

Link to comment
Share on other sites

Also let us see you cleanquote function probrably then could point out where it's adding the extra ' from... but it seems that it's still keeping this 'O'brien' I understand the last one but not sure why it's keeping the first one... either way try using str_replace(find,replace,string,count) a built in function to do this already.

Link to comment
Share on other sites

Thanks , Kinda tried that already using addslashes like mysql_real_escape_string but My query still then comes out as:

 

INSERT INTO parse_original (player1,player2) VALUES (\'Timothy\',\'O\'Brien\');

 

You are passing in your data incorrect, it should be:

 

$data = mysql_real_escape_string($data);

$query = "INSERT ... (..., '$data', ..)";

 

instead of:

 

$data = mysql_real_escape_string("'$data'");

$query = "INSERT ... (..., $data, ..)";

Link to comment
Share on other sites

Thanks Guys:

 


function cleanQuotes($string) { 
$replace_characters = array('#',"'");
$html_characters = array("","& #039;");
$cleanstring = str_replace($replace_characters,$html_characters,$string); 
return $cleanstring;
} 

foreach ( $data as $line ) {
    $val = trim ($line); 
    $arr = explode ("\t", $val); 
    $val_new = cleanQuotes(implode ("','", $arr));
    $query = "INSERT INTO parse_original (player1,player2) VALUES ('".$val_new."')"; 
print $query;
mysql_query($query) or die(mysql_error()); 
} 

 

Outputting as:

 

INSERT INTO parse_original (player1,player2) VALUES ('Timothy','O'Brien');

 

 

When it should be:

 

INSERT INTO parse_original (player1,player2) VALUES ('Timothy','O'Brien');

 

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.