1

我需要使用 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 错误!

4

4 回答 4

1

问题是 $sqlPrev 和 $sqlNext 的 " 而不是 '。{$curID} 仅适用于 ""。并且没有结束 " 或 '。

于 2012-07-27T21:09:46.407 回答
0

mysql_query一次只能执行一个查询。

基本上你只需要 3 次调用mysql_query.

$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-27T21:12:03.043 回答
0

您可能需要.在字符串文字和变量之间添加连接运算符 ( )。(在 Perl 中是必需的;我在 PHP 中做同样的事情。)

$sqlPrev = 'SELECT `id` FROM `table`
    WHERE `id` < '.$curId.' AND `catId` = '.$curCat.'
    ORDER BY `id` DESC LIMIT 1';

回显正在发送到数据库的 SQL 文本。这将揭示问题。

于 2012-07-27T21:16:20.943 回答
0

对于多个查询,您需要在调用另一个查询之前使用多查询或关闭结果。例如,如果您以面向对象的风格编写。

$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM mytable";
$result=$conn->query($sql);
echo $result->num_rows;
$sql="SELECT id FROM mytable";
$result2=$conn->query($sql);
echo $result2->num_rows; // does not work because result was not closed.

为了让它工作,写这个。

$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM mytable";
$result=$conn->query($sql);
echo $result->num_rows;
$result->close();  //*********notice this new line. result 
//needs to be closed before calling another query
$sql="SELECT id FROM mytable";
$result2=$conn->query($sql);
echo $result2->num_rows; // **this does work because previous result 
// was closed.

如果您需要一次执行多个查询,或者需要在完成前一个行的输出之前执行一个查询,请使用多查询。我使用多查询是因为第二个原因,在输出前一个的所有行之前获取另一个查询。这对于嵌套查询很重要,例如一个在另一个中间的查询,例如在论坛中的多个嵌套线程中,或者对于某些网页上使用的嵌套回复。

这是在另一个查询中间完成的一个查询的示例。

$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM mytable";
$conn->multi_query($sql);
$result=$conn->store_result();
$count=0;
while($row = $result->fetch_assoc()) {
   echo "$result['id'] $result['name'];
   if ($count==0) {
   // now you can do another query in the middle of this one
   $sql="SELECT id FROM mytable";
   $conn->multi_query($sql);
   $result2=$conn->store_result();
   $row2=$result2->fetch_assoc();
   echo "result of second query is: $row2['id'] $row2['name']";
   }
   $count=$count+1;
}
于 2017-09-26T06:11:24.910 回答