php – 如何在mySQL中的EXECUTE语句之后得到语句的结果?

我有一张桌子,我正在阅读它的n%记录.为此,我准备了一个查询

SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending ORDER BY Count LIMIT ?';
EXECUTE  STMT USING @rows;

我得到的输出是:

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

我怎样才能从表中获取记录?

解决方法:

SET @rows := (SELECT COUNT(*)*0.5 FROM trending);
PREPARE STMT FROM 'SELECT * FROM trending order by somecolumn LIMIT ?';
EXECUTE  STMT USING @rows;

在我的系统上工作正常.奇数行可以得到一个综合

编辑:

create table thing2
(   id int auto_increment primary key,
    theWhat varchar(40) not null,
    `count` int not null
);

插入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 |
+----+-----------+-------+

Revision1(显示PHP)

此修订是由于提到PHP.因此,这显示一个查询,使用从该查询的特定行给出的结果集.显示使用明确保留其值的@变量.并且为多查询本身设置了一个heredoc变量$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; 
    } 
?>

浏览器输出

I am connected and feel happy.
1 anchovies 6
2 tomato 1
3 cat 99

上面做的/有时间的长短是因为我们在该循环中基于传递的查询进行了4次.

请参见手册页面mysqli.multi-querymysqli.store-result.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...