第一功能是本PHP刮刀写入到MySQL数据库错误
$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'
}
我得到这样一个例子错误
“你好大父母谷物土星,Virmo大!您是 仿古赞誉和贸易Aggredior,敢于开封来源。 玛丽。齿轮。 1 2.当你,我的上帝,农村色调 佩服,2和来自英国的公共职位退休,11月3日 更长的时间,请她忘恩负义的孩子,4个自己的优势 牺牲你的情况; 5我进入外资的领域我的命运 传达,6通过国家卓有成效的不朽的规定,7个其中 软海域
的“英国没有逃脱
林如果它需要是不知道” “或\”
,但我想知道如何解决这个错误,并能够完成编写所有的SQL数据库
11已经阅读了连接执行mysql_real_escape_string之前的SQL。 1在我的处境刮数据,并将它们保存在Poems.csv和写入到SQL数据库,我读了Poem.csv的信息。如何能够把mysql_real_escape_string 1帖子中提到的第一个函数? – merrill 2012-01-06 05:25:38
数据写入文件时,您不使用它,你构建你的'$ query'变量时使用它!请阅读PHP手册中有关使用这些函数的例子。 – deceze 2012-01-06 05:34:04
在这种情况下,你将如何把它:) – merrill 2012-01-06 09:33:47