Consider this simple PHP page:
<?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();
?>
Well, this does not work.
either if I use query or multi_query
Can you help me?
It looks like you just want to select the 3rd row from some table. Though the rank will be nondeterministic since you don’t specify an
ORDER BYclause for that inner query.You don’t need this variable and multiple queries to do that.
Use an offset in the
LIMITclause, along with anORDER BYclause, instead.