iT邦幫忙

0

請教php程式利用curl_init來post到網頁,該如何將結果寫到資料庫

php

以下是我的程式碼 echo 結果是沒有問題 但是 要 insert 到資料庫就變成沒有辦法?請教該怎麼辦呢?

include "./include/db.php"; //這邊已經確實載入所有資料庫設定
$url="http://www.twnic.net.tw/whois.cgi"; //傳送目的網址
$postdata="query=google&sld=com.tw"; //傳送值
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_TIMEOUT,1); //Timeout 時間
$result = curl_exec($ch);
curl_close($ch);
$xxx = $result ;
echo $xxx;
//把$xxx 替代成為 任何 字串 或 數值 是沒有問題 所以資料庫寫入本身是ok的
$sql = "insert into ".$db_sht[seleve_sss]." set
content = '".$xxx."'
";
$res = mysql_query($sql);

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

4
fillano
iT邦超人 1 級 ‧ 2011-08-18 12:30:21

有東西需要escape吧?先用mysql_real_escape_string()處理一下看看。

2
andyto202
iT邦研究生 4 級 ‧ 2012-01-26 19:19:14

請問你所謂的沒有辦法是寫入空值嗎
$result = curl_exec($ch);
$result是字串而不是陣列喔
所以可能會很大
你的mysql的欄位是設varchar嗎
如果是顯示不完整的話
就是你設的太小了
建議設成
longtext
應該就可以了

我要發表回答

立即登入回答