[英]How do i get result of a statement after EXECUTE statement in mySQL?
I have a table and i am reading n% of records from it. 我有一张桌子,我正在从中读取n%的记录。 For that i have prepared a query:
为此,我准备了一个查询:
SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending ORDER BY Count LIMIT ?';
EXECUTE STMT USING @rows;
The output I get is: 我得到的输出是:
SET @rows := (SELECT COUNT(*)*0.5 FROM trending);# MySQL returned an empty result set (i.e. zero rows). PREPARE STMT FROM 'SELECT * FROM trending ORDER BY Count LIMIT ?';# MySQL returned an empty result set (i.e. zero rows). EXECUTE STMT USING @rows;# Rows: 8
How can i fetch the records from the table? 我如何从表中获取记录?
SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending order by somecolumn LIMIT ?';
EXECUTE STMT USING @rows;
works fine on my system. 在我的系统上工作正常。 odd number of rows gets you a round-up
行数奇数使您无法综合
Edit: 编辑:
create table thing2
( id int auto_increment primary key,
theWhat varchar(40) not null,
`count` int not null
);
Insert 5 rows: 插入5行:
insert thing2(theWhat,`count`) values ('anchovies',6),('tomato',1),('cat',99),('mouse',8),('spoon',70);
SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 LIMIT ?';
EXECUTE STMT USING @rows;
count might need to be in backticks (not on my system), if that is actually a column and you are doing
the following :
SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 order by `count` LIMIT ?';
EXECUTE STMT USING @rows;
+----+-----------+-------+
| id | theWhat | count |
+----+-----------+-------+
| 2 | tomato | 1 |
| 1 | anchovies | 6 |
| 4 | mouse | 8 |
+----+-----------+-------+
SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE STMT FROM 'SELECT * FROM thing2 order by theWhat LIMIT ?';
EXECUTE STMT USING @rows;
+----+-----------+-------+
| id | theWhat | count |
+----+-----------+-------+
| 1 | anchovies | 6 |
| 3 | cat | 99 |
| 4 | mouse | 8 |
+----+-----------+-------+
This revision is due to the op mentioning PHP. 此修订是由于op提及PHP。 So this shows a multi-query, using the result set given from a particular line of that query.
因此,这显示了一个多查询,使用的是从该查询的特定行给出的结果集。 Shows the use of an
@
variable that clearly retains its value. 显示使用
@
变量,该变量明显保留其值。 And a heredoc variable $theSql
being set for the multi-query itself. 和定界符变量
$theSql
被设置为多查询本身。
<?php
error_reporting(E_ALL);
//mysqli_report(MYSQLI_REPORT_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
ini_set('display_errors', 1);
try {
$mysqli= new mysqli('localhost', 'dbusername', 'thePassword', 'theDbname'); // tweak accordingly
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo "I am connected and feel happy.<br/>";
// Note the 3rd statment below (the EXECUTE) and the code below it that cares about output for the 3rd one only
$theSql = <<<SQL
SET @rows := (SELECT COUNT(*)*0.5 FROM thing2);
PREPARE stmt123 FROM 'SELECT * FROM thing2 LIMIT ?';
EXECUTE stmt123 USING @rows;
DEALLOCATE PREPARE stmt123;
SQL;
$shouldDebug=false; // change to true to see more debug info (such as the skipped multi-query results)
$theCounter=0;
$weCareAbout=3; // the 3rd line of the command: "EXECUTE stmt123 USING @rows; "
if ($mysqli->multi_query($theSql)) {
do { // Note this loop poached from http://php.net/manual/en/mysqli.multi-query.php
if ($shouldDebug) echo "1a.<br/>";
$theCounter++;
// store first result set
if ($result = $mysqli->store_result()) {
if ($shouldDebug) echo "1b.<br/>";
if ($theCounter==$weCareAbout) {
while ($row = $result->fetch_row()) {
echo $row[0]." ".$row[1]." ".$row[2]."<br>";
}
}
if ($shouldDebug) echo "1c.<br/>";
$result->free();
if ($shouldDebug) echo "1d.<br/>";
}
// print divider
if ($mysqli->more_results() && $shouldDebug) {
printf("-----------------\n");
}
if ($shouldDebug) "1e.<br/>";
} while ($mysqli->next_result() && $mysqli->more_results());
// above line to avoid error: Strict Standards: mysqli::next_result(): There is no next result set. ...
// ...Please, call mysqli_more_results()/mysqli::more_results() to check whether to call ...
//
// the Manual page is not exactly clear on this. In fact, faulty.
// http://php.net/manual/en/mysqli.multi-query.php
}
$mysqli->close(); // just showing it, what the heck
} catch (mysqli_sql_exception $e) {
throw $e;
}
?>
Browser output: 浏览器输出:
I am connected and feel happy.
1 anchovies 6
2 tomato 1
3 cat 99
The long and short of that above do/while is that we are in that loop 4 times based on the query passed. 上面do / while的长处和短处是,根据传递的查询,我们在该循环中处于4次。
Please see the Manual Page mysqli.multi-query
and mysqli.store-result
. 请参见手册页
mysqli.multi-query
和mysqli.store-result
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.