MySQL“INSERT INTO” - 错误:我必须使用哪种引用?

问题描述:

我正在为一个学校项目制作基于Faroo.com的API(http://www.faroo.com/hp/api/api.html)的搜索引擎。我想索引Faroo的索引,以便用户(在我的情况下,孩子们)可以投票或单独投票结果。MySQL“INSERT INTO” - 错误:我必须使用哪种引用?

什么我(PHP)-script是这样的:

Look in the MySQL-database if the query exists.

yes => load the results from the database and show them to the user

no => load the results from Faroo, show those results to the user and store them in the database

我的数据库看起来是这样的:

我得到存储在列从Faroo的所有数据API,'id'列除外。

(的存储在数据库中的Faroo数据)的最后一部分是哪里出了问题:

for($x=0; $x<$tel; $x++){    
    $sql = "INSERT INTO queries (`id`, `query`, `title`, `url`, `domain`, `kwic`, `votes`) VALUES (NULL, $q, $titles[$x], $urls[$x], $domains[$x], $kwics[$x], 0);"; 
    echo '<br />'.$x.'e query: @'.$sql.'@'; 
    if(!$resultaat = $db->query($sql)){ 
     die('De query kon niet worden uitgevoerd: [' . $db->error . ']'); 
    }    
    $resultaat = mysqli_fetch_array($resultaat); 
} 

$tel是计数我从Faroo得到的结果数的变量。它在这段代码之前被定义。 当我运行这段代码,我得到一个不错的MySQL错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'States Bureau of Mines - Wikipedia, the free encyclopedia, www.wikipedia.org' at line 1

我已经搜查,搜查,但我找不到SQL错误是什么。我认为这与字符串中的奇怪字符有关,或者我的引用是错误的?

亲切的问候, 最大

+1

您需要引用您的字符串值;或者甚至更好地使用[参数化查询](http://*.com/questions/728229/parameters-in-mysqli)。 –

我认为你需要使用单引号'为VARCHAR列,所以更改如下

$sql = "INSERT INTO queries (`id`, `query`, `title`, `url`, `domain`, `kwic`, `votes`) VALUES (NULL, '$q', '$titles[$x]', '$urls[$x]', '$domains[$x]', '$kwics[$x]', 0)"; 

您也有一个额外的双引号在查询结束我删除了,你不会需要单列引号列idvotes,因为它们是integer字段