我更改了代碼,以便可以直接插入數據庫。現在我遇到了一行代碼$database->bindParam(':name', $name[0][$i]);
的問題。這給了我錯誤Fatal error: Call to undefined method PDO::bindParam()
我該如何解決這個問題?使用預處理語句將數據插入數據庫pdo
$hostname = 'XXX';
$database = 'XXX';
$username = 'XXX';
$password = 'XXX';
$database = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = get_data($_GET['url']);
preg_match_all('/\<font face\=\"Arial\"\>\<strong\>(.*)\<br\>\<font color/', $html, $name);
preg_match('/\<strong\>([A-Z\s0-9]+) BIRTHDAYS\<\/strong\>/', $html, $date);
for ($i=0, $n=count($name[1]); $i<$n; ++$i) {
try {
$insert = $database->prepare("INSERT INTO bday (name, birthdate) VALUES (:name, :date)");
$database->bindParam(':name', $name[0][$i]);
$database->bindParam(':date', $date[1]);
$insert->execute();
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
有自動遞增的主鍵簡單的插入,該數據庫將採取其他 – 2011-12-19 03:31:23