Esacaping character issue while inserting into mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
atiq576
Forum Newbie
Posts: 7
Joined: Thu May 13, 2010 2:28 am

Esacaping character issue while inserting into mysql

Post by atiq576 »

I am importing different text files into the database, it is only inserting data until we get a qutoe or some other character then alphabet like this

Adsense is all about targeted content, the more targeted your content is, the more target the search engines’ ads will be. It will stop at engines.

Another example is With the real-time reporting at hand, HERE It WILL STOP AT real

I have tried different options to escape single quotes etc but this is not normal singl quote like ' its a comma on top, what to do to skip all characters like these.

Thanks,
Atiq
User avatar
Alex-V
Forum Newbie
Posts: 17
Joined: Mon Jul 19, 2010 3:53 pm

Re: Esacaping character issue while inserting into mysql

Post by Alex-V »

atiq576
Forum Newbie
Posts: 7
Joined: Thu May 13, 2010 2:28 am

Re: Esacaping character issue while inserting into mysql

Post by atiq576 »

I have already tried that function and did some more but it didn't work. It does work locally but its not working on live server.

Query is this:

$sql = "UPDATE sn_posts set post_content = '".htmlspecialchars(mysql_real_escape_string($buf))."' WHERE ID = '".$post_id."'";

$buf is the variable that is getting data from text file.

Can it be a server configuration issue.

Atiq
atiq576
Forum Newbie
Posts: 7
Joined: Thu May 13, 2010 2:28 am

Re: Esacaping character issue while inserting into mysql

Post by atiq576 »

Please read my message above, also i looked and it escapes based on characterset, we have currently utf8 both locally and on server.

\$db['default']['char_set'] = 'utf8';
\$db['default']['dbcollat'] = 'utf8_general_ci';

Its same to the server but still it doesn't work on server, can i try some thing else? Where can i check character set of the database it self?


Regards,
Atiq
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Esacaping character issue while inserting into mysql

Post by Apollo »

atiq576 wrote:Query is this:

$sql = "UPDATE sn_posts set post_content = '".htmlspecialchars(mysql_real_escape_string($buf))."' WHERE ID = '".$post_id."'";
You should not use htmlspecialchars() on strings that are to be inserted in an SQL query, only on strings that are to be outputted as HTML.

Even if you intend to do htmlspecialchars() on strings that are stored in your database (as a sort of cached pre-formatting measure, so you can retrieve them later and output as HTML directly, without doing htmlspecialchars at that time) then still you should ALWAYS apply mysql_real_escape_string() last. This holds for any other functions besides htmlspecialchars as well.

Remember: mysql_real_escape_string() makes a string suitable to be used in a query. Any other function, such as htmlspecialchars, may (and should be assumed to) mess this up, making the string no longer trustworthy to include in SQL queries.
atiq576
Forum Newbie
Posts: 7
Joined: Thu May 13, 2010 2:28 am

Re: Esacaping character issue while inserting into mysql

Post by atiq576 »

$sql = "UPDATE sn_posts set post_content = '".mysql_real_escape_string($buf)."' WHERE ID = '".$post_id."'";

As you can see above I removed the htmlspecialchars function from the query now but it still doesn't insert the full string. I am attaching a smaple file that I am testing it with

Below I am copying the paragraph that I have in file that I m importing and getting issue:

Adsense is all about targeted content, the more targeted your content is, the more target the search engines’ ads will be. There are some web masters and publishers who are focused more on their site contents and how best to maintain them rather than the cash that the ads will generate for them. This is the part where the effectiveness is working its best.

The insert stope after search engines.

Please advise.
Thanks,
Atiq
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Esacaping character issue while inserting into mysql

Post by Sindarin »

Don't you get any error messages?

Try view source and see if it's an mySQL error message in there.

I suspect some server configuration issue. Plus, did you check if magic_quotes etc are set to Off?
Post Reply