2009-10-08 46 views
0

我想保存數據到數據庫,我得到一個錯誤,我從來沒有見過 我有一個預感它與db整理有關,但我不知道什麼是什麼錯了,保存到MySQL數據庫使用PHP和mysqli

這裏是查詢:

$query1 = "INSERT INTO scape.url (url,normalizedurl,service,idinservice) VALUES (url, normalizedurl, 4, 45454)"; 
$query = "INSERT INTO scape.url (url, normalizedurl, service, idinservice) VALUES (" 
      .$sql->real_escape_string($this->url)."," 
      .$sql->real_escape_string($this->normalizedUrl)."," 
      .$sql->real_escape_string($this->service)."," 
      .$sql->real_escape_string($this->idInService).")"; 
$result = $sql->query($query); 
echo $sql->error; 

錯誤消息我得到的是:

您的SQL語法錯誤;檢查對應於你的MySQL服務器版本使用附近的正確語法手冊「://www.something/here/here/here/12345,httpwwwsomthighere」在行1

數據庫歸類爲這個領域是utf8-general-ci和字段類型是varchar 255

對此有何看法?

回答

1

如果你這樣做,你仍然需要引用字符串(url和normalizedurl)。這是它所指的語法問題。

令人傷心的是,這不是使用mysqli傳遞參數的推薦方式。 mysqli的重點在於它具有查詢參數化。例如:

$mysqli = new mysqli($host, $user, $password, $database); 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit; 
} 
$sql = <<<END 
INSERT INTO scape.url (url,normalizedurl,service,idinservice) 
VALUES (?, ?, ?, ?) 
END; 
$stmt = $mysqli->prepare($sql); 
if ($stmt === false) { 
    printf("Error executing %s: %s\n", $sql, $stmt->error); 
    exit; 
} 
$stmt->bind_param('ssii', $this->url, $this->normalizedUrl, 
    $this->service, $this->idInService); 
$stmt->execute(); 
+0

感謝您的克萊 如果我能我會彈出「你剛剛贏得了大師徽章」在屏幕上:) 一如既往你的回答是詳細而準確的 – 2009-10-08 02:13:02