mysql_real_escape_string


Posted on 16th Feb 2014 07:03 pm by admin

Let me preface this that I am very much a PHP noob, but I have some SQL training (not necessarily MySQL, we used the Microsoft variety in school). I have a weird problem, I'm trying to create a script that will take a RSS feed and import it to a MySQL database. It worked well when I was only bringing in 10 items in the feed at a time, but when I opened it up to a larger set of items it freaks out. I think it has to do with the data not being very "clean". Meaning the data has a lot of single and double quotes and what not. As you'll see in my code I've tried to fix this by using mysql_real_escape_string(), but it still get's hung up for some reason. Here's a look at my code:
Code: <?php include('rss_fetch.inc');
define('MAGPIE_FETCH_TIME_OUT', 200);

function safe($value){
return mysql_real_escape_string($value);
}

// Set error reporting for this
ini_set('display_errors',1);
error_reporting(E_ALL);

// Fetch RSS feed

$rss = fetch_rss('feedurl');



if ($rss) {
// Split the array
$items = array_slice($rss->items,0);

// Cycle through each item and echo
foreach ($items as $item)
{
$con = mysql_connect("server","uname","pword");
if (!$con)
{
die('MySQL could not connect: ' . mysql_error());
}

mysql_select_db("rsstomysql", $con);


$sql="INSERT INTO `opportunities` (`opp_id`, `opp_title`, `opp_link`, `opp_desc`, `opp_provider`, `opp_startdate`, `opp_enddate`, `opp_location_name`, `opp_categories`, `opp_openended`, `opp_sponsororg`, `opp_starttime`, `opp_endtime`, `opp_contactemail`, `opp_contactphone`, `opp_contactname`)
VALUES('".$item['fp']['id']."', '".safe($item['title'])."', '".$item['link']."', '".safe($item['description'])."', '".$item['fp']['provider']."', '".$item['fp']['startdate']."', '".$item['fp']['enddate']."', '".$item['fp']['location_name']."', '".$item['fp']['categories']."', '".$item['fp']['openended']."', '".$item['fp']['sponsoringorganizationname']."', '".$item['fp']['starttime']."', '".$item['fp']['endtime']."', '".$item['fp']['contactemail']."', '".$item['fp']['contactphone']."', '".$item['fp']['contactname']."')
ON DUPLICATE KEY UPDATE `opp_title` = '".$item['title']."';";
if (!mysql_query($sql,$con))
{
die('MySQL Error: ' .mysql_errno() .mysql_error());
}
echo "Records added";
mysql_close($con);
}
}
else
{
echo '<h2>Magpie Error:</h2><p>'.magpie_error().'</p>';
}
// Restore original error reporting value
@ini_restore('error_reporting');
?>This is the error I'm getting:
QuoteMySQL Error: 1064You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Stop Modern Day Slavery'' at line 3The 'Stop Modern Day Slavery" is in one of the title fields in the feed. I have used mysql_real_escape_string() on the title field, but it does not seem to have any effect. Does anyone have any ideas? I've also used addslashes() and it does the same thing...

No comments posted yet

Your Answer:

Login to answer
72 Like 34 Dislike
Previous forums Next forums
Other forums

Intrastat Report Config in ECC 6.0 - goods Movement in EU countries
Colleagues, need ur help to understand the changes require in ECC 6 standard Report configuration se

parsing error
can anyone explain this syntax error:

Parse error: syntax error, unexpected T_IF

The e

Baffled by Undefined Index in Simple Array: Please Help!
Hello. I have a form which posts an array to this script. However, I can't seem to access the values

Searching keywords from array
Hello everyone, Im just wondering if someone could help me with another issue im having. Im still le

PHP Function Page Advice
Hi All, I am quite new to PHP and Javascript but have written an Online Shop (Mainly for Fun and edu

question about stripslashes and real_escape_string
im cleaning up an old app that I wrote fixing some of the vulernabilities from attacks.

I hav

Unifying logins of two different scripts
I have two different game scripts which I want to embed on my own site. The problem is I don't want

Interpret Order
Hello all,

I'm wondering if I have this:

Code: $switch = array(
'one' =>

mysql VARCHAR acting like INT
Hi, All.

I have a table that contains a varchar(10) column named weird_field. In this column

Map.php
I am a complete beginner to PHP and am looking for some help with a program I am messing around with

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash