[請益] PHP 執行exec()的問題

看板PHP作者 (dick)時間13年前 (2012/08/27 17:08), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
我想要用php來執行exec() 執行perl 先把資料庫裡存的網址抓下來 再去該網頁抓取內容 存進資料庫 我用cmd執行可以成功存進資料庫 但我用php去呼叫exec()函數 就無法將內容存進資料庫 但也沒有顯示錯誤訊息 想請問哪裡出錯了??? 以下是我的程式碼 ----------getnews.php---------------- $x=exec('cd desktop');//檔案在desktop $z=exec('perl test1.pl'); print($x); print($z); ---------test1.pl--------------------- use DBI; my $dbh=DBI->connect("dbi:mysql:dbname=xxx","xxx",'xxx');#開啟資料庫 $sth = $dbh->do( "SET character_set_client ='utf8'"); $sth = $dbh->do( "SET character_set_connection='utf8'"); $sth = $dbh->do( "SET character_set_results='utf8'"); $sql1="select id from newsurl ORDER BY id DESC" ; $k=$dbh->prepare($sql1); $k->execute(); $a= $k->fetchrow_array(); $k->finish(); for($x=0;$x<=5;$x++){ $sql2="select url from newsurl where id='$a'" ; $a-=1; $k1=$dbh->prepare($sql2); $k1->execute(); @result[$x] = $k1->fetchrow_array(); } $k1->finish(); use WWW::Mechanize; # 使用這模組 use encoding 'utf-8'; my $agent1 = WWW::Mechanize->new(); for($y=0;$y<=5;$y++){ $agent1 -> get(@result[$y]); my $content1 = $agent1 ->{content}; @title= $content1=~/<h1 class=\"headline\">(.*?)<\/h1>/g; @time= $content1=~/\&ndash\;\&nbsp\;<abbr title=\"(.*?)T/; @article= $content1=~/<p class=\"first\">(.*)<\/p>/g; $sql3="insert into test1 (title, content, time) values ('@title','@article','@time')";#sql指令 $dbh->do( $sql3 ); } $dbh->disconnect;#關閉資料庫 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.175.221

08/27 19:25, , 1F
權限沒開,去把apache這個user加進sudoers裡
08/27 19:25, 1F

08/29 10:36, , 2F
感謝J大解答 已解決 ^^
08/29 10:36, 2F
文章代碼(AID): #1GEpaG-H (PHP)
文章代碼(AID): #1GEpaG-H (PHP)