pref_split problems

Discussion in 'PHP' started by cscott5288, Nov 1, 2011.

  1. #1
    Okay so I've got this very simply preg_split function searching for a semicolon (';') in a very long string (that is actually an sql query exported from mySQL).

    $queries = preg_split('/;/',$sql);
    PHP:
    The string it is searching through ($sql):

    $sql = "
    -- phpMyAdmin SQL Dump
    -- version 3.4.5
    -- [url]http://www.phpmyadmin.net[/url]
    --
    -- Host: localhost
    -- Generation Time: Oct 30, 2011 at 09:38 AM
    -- Server version: 5.1.56
    -- PHP Version: 5.2.9
    
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";
    
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    
    --
    -- Database: `newssc3_bt`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `wp_links`
    --
    
    CREATE TABLE IF NOT EXISTS `wp_links` (
      `link_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
      `link_url` varchar(255) NOT NULL DEFAULT '',
      `link_name` varchar(255) NOT NULL DEFAULT '',
      `link_image` varchar(255) NOT NULL DEFAULT '',
      `link_target` varchar(25) NOT NULL DEFAULT '',
      `link_description` varchar(255) NOT NULL DEFAULT '',
      `link_visible` varchar(20) NOT NULL DEFAULT 'Y',
      `link_owner` bigint(20) unsigned NOT NULL DEFAULT '1',
      `link_rating` int(11) NOT NULL DEFAULT '0',
      `link_updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
      `link_rel` varchar(255) NOT NULL DEFAULT '',
      `link_notes` mediumtext NOT NULL,
      `link_rss` varchar(255) NOT NULL DEFAULT '',
      PRIMARY KEY (`link_id`),
      KEY `link_visible` (`link_visible`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ;
    
    --
    -- Dumping data for table `wp_links`
    --
    
    INSERT INTO `wp_links` (`link_id`, `link_url`, `link_name`, `link_image`, `link_target`, `link_description`, `link_visible`, `link_owner`, `link_rating`, `link_updated`, `link_rel`, `link_notes`, `link_rss`) VALUES
    (22, 'http://kikolani.com/', 'Kikolani', '', '', '', 'Y', 1, 0, '0000-00-00 00:00:00', '', '', ''),
    (23, 'http://jobhits.net/blog/', 'Job Search Blog', '', '', 'Lastest news and updates recruitment and employment.', 'Y', 2, 0, '0000-00-00 00:00:00', '', '', ''),
    (24, 'http://jobhits.net/Seo-jobs', 'Seo Jobs', '', '', 'Seo Jobs', 'Y', 2, 10, '0000-00-00 00:00:00', '', '', 'http://jobhits.net/services/rss?k=seo'),
    (21, 'http://www.webuildyourblog.com/', 'Blogging Guide', '', '', '', 'Y', 1, 0, '0000-00-00 00:00:00', '', '', ''),
    (17, 'http://ma.tt/', 'Matt Mullenweg', '', '', '', 'Y', 1, 0, '0000-00-00 00:00:00', '', '', ''),
    (19, 'http://www.blogussion.com/', 'Blogussion', '', '', '', 'Y', 1, 0, '0000-00-00 00:00:00', '', '', ''),
    (20, 'http://weblogbetter.com/', 'We Blog Better', '', '', '', 'Y', 1, 0, '0000-00-00 00:00:00', '', '', ''),
    (25, 'http://www.buyrealfacebookfans.com', 'Get Facebook Fans', '', '', '', 'Y', 1, 0, '0000-00-00 00:00:00', '', '', ''),
    (26, 'http://www.cssformatter.com/', 'Format CSS', '', '', '', 'Y', 1, 0, '0000-00-00 00:00:00', '', '', ''),
    (27, 'http://wwww.place.vn/', 'Vietnam Place', '', '_blank', 'Vietnam Places', 'Y', 2, 0, '0000-00-00 00:00:00', '', '', '');
    
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    ";
    PHP:
    The idea is, ultimately, to use the array created by pre_split to break up the very large query into individual queries I can send through with the mysql_query() function, but I'm not there yet. My problem is that preg_split trips when it reaches this part:

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";
    PHP:
    Why?!?
    If I take that part out, preg_split functions properly and splits the giant string just the way I want it. But it won't process the above lines. if I include those lines it doesn't store anything into the array. any help would be greatly appreciated ...
     
    cscott5288, Nov 1, 2011 IP
  2. geforce

    geforce Active Member

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #2
    This is happening because you have double quotes in your query. Your $sql variable is encasing the sring in double quotes, so this is ending that string and will cause problems.

    Your line:
    
    SET SQL_MODE[COLOR=#339933]=[/COLOR][COLOR=#0000ff]"NO_AUTO_VALUE_ON_ZERO"[/COLOR][COLOR=#339933];[/COLOR]
    SET time_zone [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]"+00:00"[/COLOR][COLOR=#339933];
    [/COLOR]
    Code (markup):


    Change to:
    
    SET SQL_MODE[COLOR=#339933]=[/COLOR][COLOR=#0000ff]\"NO_AUTO_VALUE_ON_ZERO\"[/COLOR][COLOR=#339933];[/COLOR]
    SET time_zone [COLOR=#339933]=[/COLOR] \[COLOR=#0000ff]"+00:00\"[/COLOR][COLOR=#339933];
    [/COLOR]
    Code (markup):
    That should work :)
     
    geforce, Nov 2, 2011 IP