-1

第一个功能是这个

              $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

最后一个功能是

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'
 }

我收到一个像这样的错误示例

'Salve magna parens frugum Saturnia tellus,Magna virm!tibi res antiqu laudis et artis Aggredior, sanctos ausus recludere fontes。处女。乔治。2. 1 当你,我的主,乡下人羡慕, 2 从不列颠尼亚的公共职位退休, 3 不再取悦她忘恩负义的儿子, 4 为了他们的利益牺牲你的安逸;5 我进入我的命运所传达的异域, 6 穿过多产不朽的国家, 7 柔软的海洋

Britannia 中的 ' 没有逃脱

我不确定它是否需要是 '' 或 \'

但我想知道如何解决这个错误并能够完成在sql db中写入所有内容

4

3 回答 3

3

您应该使用 PHP 的mysql_real_escape_string函数来转义您插入的字符串。

于 2012-01-05T22:42:32.987 回答
3

mysql_real_escape_string在将值插入查询之前使用它们。
更好的是,与时俱进,将PDO扩展与准备好的语句一起使用。

于 2012-01-05T22:43:00.273 回答
0

这是我传递所有数据的函数,然后将其插入我的数据库......

private function makeSqlSafe($data){
  $str = @trim($data);
  if(get_magic_quotes_gpc()){
    $data = stripslashes($data);
  }
  if(!is_numeric($data)){
    $data = "'".mysql_real_escape_string($data)."'";
  }
  return $data;
}

希望有帮助...

于 2012-01-05T22:48:18.103 回答