2013-04-06 42 views
-2

问题是我认为我做错了什么,我只是不知道是什么。我试过try/catch,这段代码的其他部分都正常工作(我回应了所有内容),但没有任何内容插入到“文本”中。我99%肯定它不是特权,也是一个普遍的问题,当你只是在服务器和其他网站上搜索数据时使用exec可以吗?希望得到一些答复。简单执行插入

<?php 
include('simplehtmldom_1_5/simple_html_dom.php'); 
$html = new simple_html_dom(); 
$html->load_file('http://www.youtube.com/live/all/videos?sort=dd&tag_id=&view=41'); 
$count = 0; 
$pdo = new PDO('mysql:host=localhost;dbname=scraper', 'user', 'aa'); 
$string = $pdo->query('SELECT text FROM urls'); 
while ($row = $string->fetch()) 
{ 
    $links[] = $row['text']; 
} 
foreach($html->find('[class=content-item-title yt-ui-ellipsis yt-ui-ellipsis-2]') as $element) 
{ 
$array[$count]=$element->href; 
foreach ($links as $link) 
{ 
if (strpos($array[$count], $link) == false) 
{ 
$pdo->exec('INSERT INTO urls SET 
text=$array[$count]'); 
} 
} 
$count += 1; 
} 
?> 
+0

您的[插入声明](http://dev.mysql.com/doc/refman/5.5/en/insert.html)是错误的,您不会利用[PDO](http:// www .php.net/manual/en/book.pdo.php)并对查询进行参数化,如果您希望通过将输入直接放入查询中来允许SQL注入,则需要使用''。 $ array [$ count]。'\'''标记为非建设性。 – Jon 2013-04-06 03:31:53

回答

0

取而代之的是

$pdo->exec('INSERT INTO urls SET text=$array[$count]'); 

使用本

$pdo->prepare('INSERT INTO urls SET text=?')->execute(array($array[$count])); 

乘坐时间和阅读有关prepare in PDO。也关于Variable parsing