2011-07-25 31 views
1

考虑这个简单的PHP页面:多个查询不起作用

<?php 

    $db = new mysqli("localhost", "myuser", "mypwd", "mydb"); 
    if ($db->connect_error) 
     // Dying sequence; 

    // Executing query 
    $qres = $db->multi_query("SET @rank = -1; SELECT * FROM (SELECT @rank := @rank + 1 as rank, field1, field2 FROM mytable WHERE field1 = 'value') AS T1 WHERE rank = 2;"); 
    $db->commit(); 
    if (!qres) { 
     // Problems in query 
     // Dying 
     $db->close(); 
     return; 
    } 
    if (!($qres->num_rows == 1)) { 
     // Error fetched 
     $numrows = $qres->num_rows; 
     $db->close(); 
     // Dying 
     return; 
    } 

    // Returning 
    echo "ALLOK"; 

    $db->close(); 

    ?> 

好了,这是行不通的。 如果我使用querymulti_query

你能帮我吗?

+3

“if(!qres)”可能是致命的错字。 –

回答

1

看起来你只是想从某个表中选择第3行。虽然排名不确定,因为您没有为该内部查询指定ORDER BY子句。

你不需要这个变量和多个查询来做到这一点。

改为使用LIMIT子句中的偏移量以及ORDER BY子句。

SELECT field1, field2 FROM mytable WHERE field1 = 'value' ORDER BY something LIMIT 2,1