Search code examples
phpsqlmysqldatabase

php scrape to write to mysql database error


first function is this

              $output_csv = fopen( "Poems.csv", "w" );
..code here
               $poem_text = trim($pre->innertext);

               $poem_text = str_replace('"','',$poem_text);

               $poem_text = html_entity_decode($poem_text);

               $poem_text = str_replace(array('\\', "\0", "\n", "\r", "\t", "\x1a")," ",$poem_text);
...code here
              $p

    oem_data = array( $author_names[$i], $poem_names[$i], $poem_text, $poem_urls[$i] );
                 fputcsv( $output_csv, $poem_data );   
...code here

the last function is

function writeQuotestoDb() {
  $input_csv = fopen( "Poems.csv", "r" );
  $author_names = array();
  $poem_names = array();
  $poem_text = array();
  $poem_urls = array();

  while (($columns = fgetcsv($input_csv, 1000, ",")) !== FALSE) {
    array_push( $author_names, $columns[0] );
    array_push( $poem_names,  $columns[1] );
    array_push( $poem_text, $columns[2]);
    array_push( $poem_urls,  $columns[3] );
  }
  fclose( $input_csv );

  //$dbh = mysql_connect( 'localhost', 'poetiv', 'alegado' );
  $dbh = mysql_connect( 'localhost', 'root', '' );
  mysql_select_db( 'test', $dbh );
  mysql_query("CREATE TABLE  IF NOT EXISTS `poem2` (
             `id` INT( 20 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
             `author_name` varchar(128) DEFAULT NULL,
              `poem_name` varchar(128) DEFAULT NULL,
              `poem_text` text,
              `poem_url` varchar(1024) DEFAULT NULL
             ) ENGINE = MYISAM");
    mysql_query("TRUNCATE `poem2`");     
    for ( $i=0; $i<count($author_names); $i++ ) {
        $poems[$i] = substr($poems[$i],7,(strlen($poems[$i])-14));
        $poems[$i] = str_replace('"','',$poems[$i]);
        $query = "insert into poem2 (author_name,poem_name,poem_text,poem_url) values 
        ('" . $author_names[$i] . "', '" . $poem_names[$i]."', '" . $poem_text[$i]."', '" .$poem_urls[$i] ."')";
        if( !mysql_query($query) ) { echo $query; 
            }
        }
    echo count($author_names)." rows....successfully db updated";
    //SELECT count(*) FROM `quotes` WHERE criteria = 'Baby Quotes'
 }

i get an error like this one example

'Salve magna parens frugum Saturnia tellus, Magna virm! tibi res antiqu laudis et artis Aggredior, sanctos ausus recludere fontes. Virg. Geor. 2.   1           While you, my Lord, the rural shades admire, 2     And from Britannia's public posts retire, 3     Nor longer, her ungrateful sons to please, 4     For their advantage sacrifice your ease;  5           Me into foreign realms my fate conveys, 6     Through nations fruitful of immortal lays, 7     Where the soft seas

The ' in Britannia's is not escaped

im not sure if it needs to be ''or \'

but i would like to know how to solve this error and be able to complete to write all in the sql db


Solution

  • Use mysql_real_escape_string on your values before plugging them into a query.
    Better yet, get with the times and use the PDO extension with prepared statements.