I wrote a PHP script to import topics and posts into a new forum from an old one, I am getting a syntax error on line 2 of my SQL statement, yet I don't see what the error could be.
INSERT INTO `uh46v_chronoengine_forums_topics` (`id`,`forum_id`,`title`,`alias`,`user_id`,`published`,`locked`,`created`,`modified`,`hits`,`params`,`post_count`,`last_post`,`reported`,`has_attachments`,`announce`,`sticky`) VALUES (`33`,`2`,`DS4Windows rebranding`,`DS4Windows-rebranding`,`2`,`1`,`0`,`2015-02-04 22:10:57`,``,`0`,`{"uid":"4ad6a17c-c7e0-4626-95ea-1a248caaf964"}`,`24`,`540`,`0`,`0`,`0`,`0`);
INSERT INTO `uh46v_chronoengine_forums_posts` (`id`,`topic_id`,`forum_id`,`subject`,`text`,`user_id`,`created`,`modified`,`published`,`params`) VALUES (`172`,`33`,`2`,`DS4Windows rebranding`,`So it was suggested in out old forum that a rebranding was in order for us, mainly to differentiate ourselves from Jays distribution. With the building of forums and more traffic now coming to the site I believe splitting not only from the DS4Windows name, but also from the DSDCS brand would be a good idea sometime in the near future. Not being a very good "marketing guy" myself, im not quite sure what to rename the project. So I will open the floor on the subject and hope we have somebody in the community good with such things.
General guidelines:
Must not violate any copyrights or trademarks
Keep in mind this application may not alwayse be limited to DS4 -> X360 mapping
Prefer the .com of the name be available`,`2`,`2015-02-04 22:10:57`,``,`1`,`{"author_address":"96.58.100.87"}`);
This yields the same error
INSERT INTO `uh46v_chronoengine_forums_topics` (`id`,`forum_id`,`title`,`alias`,`user_id`,`published`,`locked`,`created`,`modified`,`hits`,`params`,`post_count`,`last_post`,`reported`,`has_attachments`,`announce`,`sticky`) VALUES ('33','2','DS4Windows rebranding','DS4Windows-rebranding','2','1','0','2015-02-04 22:10:57','','0','{"uid":"0832fbee-506c-4fac-b2f6-eda324c54580"}','24','540','0','0','0','0');
INSERT INTO `uh46v_chronoengine_forums_posts` (`id`,`topic_id`,`forum_id`,`subject`,`text`,`user_id`,`created`,`modified`,`published`,`params`) VALUES ('172','33','2','DS4Windows rebranding','So it was suggested in out old forum that a rebranding was in order for us, mainly to differentiate ourselves from Jays distribution. With the building of forums and more traffic now coming to the site I believe splitting not only from the DS4Windows name, but also from the DSDCS brand would be a good idea sometime in the near future. Not being a very good "marketing guy" myself, im not quite sure what to rename the project. So I will open the floor on the subject and hope we have somebody in the community good with such things.
General guidelines:
Must not violate any copyrights or trademarks
Keep in mind this application may not alwayse be limited to DS4 -> X360 mapping
Prefer the .com of the name be available','2','2015-02-04 22:10:57','','1','{"author_address":"96.58.100.87"}');
Error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'INSERT INTO uh46v_chronoengine_forums_posts
(id
,topic_id
,forum_id
,`subje' at line 2
For reference but not likely related since the error is a MySQL one, here is the PHP script I am using to accomplish this.
<?PHP
function GUID()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
include_once('configuration.php');
$config = new JConfig;
$servername = $config->host;
$username = $config->host;
$password = $config->host;
$sourceForumID = 7;
$destinationForumID = 2;
// Create connection
$db = new mysqli($config->host, $config->user, $config->password, $config->db);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
$topicsTableA = "phpbb_topics";
$topicsTableB = $config->dbprefix."chronoengine_forums_topics";
$postsTableA = "phpbb_posts";
$postsTableB = $config->dbprefix."chronoengine_forums_posts";
$topicsSQLA = <<<SQL
SELECT *
FROM `$topicsTableA`
WHERE forum_id = $sourceForumID
SQL;
if(!$topicsResultA = $db->query($topicsSQLA)){
die('There was an error running the query [' . $db->error . ']');
}
$topicsSQLB = "";
while($topicsRowA = $topicsResultA->fetch_assoc()){
$id = $topicsRowA["topic_id"];
$postsSQLA = <<<SQL
SELECT *
FROM `$postsTableA`
WHERE topic_id = $id
SQL;
if(!$postsResultA = $db->query($postsSQLA)){
die('There was an error running the query [' . $db->error . ']');
}
$id = $topicsRowA["topic_id"];
$forum_id = $destinationForumID;
$title = mysqli_real_escape_string($db,$topicsRowA["topic_title"]);
$alias = preg_replace("/[^A-Za-z0-9]/", '-', $topicsRowA["topic_title"]);
$user_id = $topicsRowA["topic_poster"];
$published = 1;
$locked = 0;
$created = (new DateTime("@".$topicsRowA["topic_time"]))->format('Y-m-d H:i:s');
$modified = null;
$hits = 0;
$params = mysqli_real_escape_string($db,'{"uid":"'.strtolower(GUID()).'"}');
$post_count = $postsResultA->num_rows;
$last_post = $topicsRowA["topic_last_post_id"];
$reported = 0;
$has_attachments = 0;
$announce = 0;
$sticky = 0;
$topicsSQLB .= "INSERT INTO `$topicsTableB` (`id`,`forum_id`,`title`,`alias`,`user_id`,`published`,`locked`,`created`,`modified`,`hits`,`params`,`post_count`,`last_post`,`reported`,`has_attachments`,`announce`,`sticky`) VALUES ('$id','$forum_id','$title','$alias','$user_id','$published','$locked','$created',NULL,'$hits','$params','$post_count','$last_post','$reported','$has_attachments','$announce','$sticky');
";
while($postsRowA = $postsResultA->fetch_assoc()){
$Tid = $postsRowA["post_id"];
$topic_id = $id;
$subject = mysqli_real_escape_string($db,$postsRowA["post_subject"]);
$text = mysqli_real_escape_string($db,$postsRowA["post_text"]);
$user_id = $postsRowA["poster_id"];
$created = (new DateTime("@".$postsRowA["post_time"]))->format('Y-m-d H:i:s');
$modified = null;
$published = 1;
$params = mysqli_real_escape_string($db,'{"author_address":"'.$postsRowA["poster_ip"].'"}');
$topicsSQLB .= "INSERT INTO `$postsTableB` (`id`,`topic_id`,`forum_id`,`subject`,`text`,`user_id`,`created`,`modified`,`published`,`params`) VALUES ('$Tid','$topic_id','$forum_id','$subject','$text','$user_id','$created',NULL,'$published','$params');
";
}
}
//if ($db->query($topicsSQLB) === true) {
// echo "New record created successfully";
//} else {
// echo "Error: " . $sql . "<br>" . $db->error . "<br>";
//}
echo $topicsSQLB;
See Question&Answers more detail:
os