我需要使用PHP运行多个Mysql查询。我有一个网站,并从数据库中提取所有信息
$sql = "SELECT * FROM $table WHERE ID=$escape";
$query = mysql_query($sql) or die(mysql_error());
$rentals = mysql_fetch_assoc($query);现在,我还有另外两个查询需要运行,分别对应上一步和下一步按钮
$sqlPrev = 'SELECT `id` FROM `table`
WHERE `id` < '$curId' AND `catId` = '$curCat'
ORDER BY `id` DESC LIMIT 1;
$sqlNext = 'SELECT `id` FROM `table`
WHERE `id` > '$curId' AND `catId` = '$curCat'
ORDER BY `id` ASC LIMIT 1;当我在PHP MyAdmin中运行它们时,我有正确的代码,但是当我试图通过网站执行它们时,我得到了一个mysql错误!
发布于 2012-07-28 05:12:03
mysql_query一次只能执行一个查询。
基本上,您只需要对mysql_query进行3次调用。
$sql = "SELECT * FROM $table WHERE ID=$escape";
$query = mysql_query($sql) or die(mysql_error());
$rentals = mysql_fetch_assoc($query);
$sqlPrev = 'SELECT `id` FROM `table`
WHERE `id` < ' . $curId . ' AND `catId` = ' . $curCat . '
ORDER BY `id` DESC LIMIT 1';
$sqlNext = 'SELECT `id` FROM `table`
WHERE `id` > ' . $curId . ' AND `catId` = ' . $curCat . '
ORDER BY `id` ASC LIMIT 1';
$resultPrev = mysql_query($sqlPrev);
$resultNext = mysql_query($sqlNext);
// todo: check that the above queries executed successfully
// if (!$resultPrev) echo mysql_error();
if (mysql_num_rows($resultPrev)) {
$prev = mysql_fetch_array($resultPrev);
$prevId = $prev['id'];
} else {
$prevId = null; // there is no previous item
}
if (mysql_num_rows($resultNext)) {
$next = mysql_fetch_array($resultNext);
$nextId = $next['id'];
} else {
$nextId = null; // there is no next item
}发布于 2012-07-28 05:09:46
问题是“在$sqlPrev和$sqlNext,而不是‘。{$curID}只适用于"”。并且没有结尾“或”。
发布于 2012-07-28 05:16:21
您可能需要在字符串文字和变量之间添加连接运算符(.)。(这在Perl中是必需的;我在PHP中也是这样做的。)
$sqlPrev = 'SELECT `id` FROM `table`
WHERE `id` < '.$curId.' AND `catId` = '.$curCat.'
ORDER BY `id` DESC LIMIT 1';回显发送到数据库的SQL文本。这将揭示问题所在。
https://stackoverflow.com/questions/11695637
复制相似问题