[英]Insert statement not working in foreach statement
I have a crawler which crawls the sites that begin with www.bbc.co.uk/news. 我有一个搜寻器,可搜寻以www.bbc.co.uk/news开头的网站。 It it grabs all the links that start with http://www.bbc.co.uk/news and finds their description, link and title and inserts them into a database.
它捕获以http://www.bbc.co.uk/news开头的所有链接,并找到它们的描述,链接和标题,并将其插入数据库中。
For some reason, it doesn't seem to be inserting. 由于某种原因,它似乎没有插入。
Any ideaS? 有任何想法吗?
PS There is completely not output, completely blank web page PS完全没有输出,网页完全空白
foreach ($links as $link) {
$output = array(
"title" => Titles($link), //dont know what Titles is, variable or string?
"description" => getMetas($link),
"keywords" => getKeywords($link),
"link" => $link
);
if (empty($output["description"])) {
$output["description"] = getWord($link);
}
if (substr($ouput, 0, 26) == "http://www.bbc.co.uk/news/") {
$data = '"' . implode('" , "', $output) . '"';
$success = mysql_query( "INSERT INTO news_story (`title`, `description` , `keywords`, `link`)
VALUES (" . $data . ")") or zerror_reporting();
if ($sucess) {
echo "YEAH!";
}
if (!$sucess) {
echo "NO!!";
}
print_r($data);
}}
Problem is Here: 问题在这里:
if (substr($ouput, 0, 26) == "http://www.bbc.co.uk/news/") {
$data = '"' . implode('" , "', $output) . '"';
$success = mysql_query( "INSERT INTO news_story (`title`, `description` , `keywords`, `link`)
VALUES (" . $data . ")") or zerror_reporting();
if ($sucess) {
echo "YEAH!";
}
Where is your $ouput
variable...I think you wanted to write $output
..but it also didnot execute because $output
variable is a array not a string
$ouput
变量在哪里...我想您想写$output
..但它也没有执行,因为$output
变量是一个数组而不是string
The blank white page is a PHP Fatal error that produces a 500 Internal Server Error
response. 空白页是PHP致命错误,会产生
500 Internal Server Error
响应。 That is caused by this undefined function zerror_reporting()
: 这是由于此未定义的函数
zerror_reporting()
引起的:
mysql_query(...) or zerror_reporting();
Change that to something like 将其更改为类似
mysql_query(...) or trigger_error(mysql_error());
The trigger_error() call will add the mysql error to your error log. trigger_error()调用会将mysql错误添加到您的错误日志中。
The second problem is you're trying to substr()
on an array, you should be doing that on the link
element: 第二个问题是您尝试在数组上使用
substr()
,应该在link
元素上执行此操作:
if (substr($output['link'], 0, 26) == "http://www.bbc.co.uk/news/") {
在插入数据库之前对您的值进行消毒
Solution by @Mrinmoy is correct but there seems more issues in the code since your code never touched this further. @Mrinmoy的解决方案是正确的,但是代码中似乎还有更多问题,因为您的代码再也没有涉及到这一点。
First set display errors: 首先设置显示错误:
ini_set('error_reporting',E_ALL);
ini_set('display_errors','on');
foreach ($links as $link) {
PHP talks a lot, if you can listen. 如果您能听,PHP会说很多话。 I personally use E_ALL|E_STRICT but thats little too much for today.
我个人使用E_ALL | E_STRICT,但是今天太少了。 :) Then sanitize your data or you would rarely succeed in inserting records.
:)然后清理您的数据,否则您将很难成功插入记录。 Your data will have lot of sentences:
您的数据将包含很多句子:
$output = array(
"title" => mysql_real_escape_string(Titles($link)), //dont know what Titles is, variable or string?
"description" => mysql_real_escape_string(getMetas($link)),
"keywords" => mysql_real_escape_string(getKeywords($link)),
"link" => mysql_real_escape_string($link)
);
if (empty($output["description"])) {
$output["description"] = mysql_real_escape_string(getWord($link));
}
Then correct the variable typo and use the link index of the output array: 然后更正变量错字并使用输出数组的链接索引:
if (substr($output['link'], 0, 26) == "http://www.bbc.co.uk/news/") {
At the end, if you still don't get data, you definitely will know much more to fix it yourself. 最后,如果您仍然无法获得数据,那么您肯定会知道自己要修复的更多信息。 And use
print_r($output); echo $data;
并使用
print_r($output); echo $data;
print_r($output); echo $data;
before calling mysql_query. 在调用mysql_query之前。 Another way to track progress by filling code with
echo __LINE__ . "\\n";
通过使用
echo __LINE__ . "\\n";
填充代码来跟踪进度的另一种方法echo __LINE__ . "\\n";
echo __LINE__ . "\\n";
to see where it died. 看看它死在哪里。 Verify there is a method in your code by the name
zerror_reporting
or replace with die(mysql_error());
验证代码中是否存在名称为
zerror_reporting
的方法,或替换为die(mysql_error());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.