2013-04-06 47 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