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

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.

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.
";

// Note the 3rd statment below (the EXECUTE) and the code below it that cares about output for the 3rd one only

$theSql = <<

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.
";

$theCounter++;

// store first result set

if ($result = $mysqli->store_result()) {

if ($shouldDebug) echo "1b.
";

if ($theCounter==$weCareAbout) {

while ($row = $result->fetch_row()) {

echo $row[0]." ".$row[1]." ".$row[2]."
";

}

}

if ($shouldDebug) echo "1c.
";

$result->free();

if ($shouldDebug) echo "1d.
";

}

// print divider

if ($mysqli->more_results() && $shouldDebug) {

printf("-----------------\n");

}

if ($shouldDebug) "1e.
";

} 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-query和mysqli.store-result.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值