使用PHP运行多个Mysql查询

时间:2012-07-27 21:05:42

标签: php mysql

我需要使用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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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
}

答案 2 :(得分:0)

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

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

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

答案 3 :(得分: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;
}