Mysqli query with a SET variable statment (ie multiple queries)(带有 SET 变量语句的 Mysqli 查询(即多个查询))
问题描述
我想设置一个变量以在 mysqli 查询中使用.这不太行.在 mysqli 之前,我曾经设置查询调用.我玩 db->multi_query($sql) 没有运气.任何人都知道如何使这项工作包括 set 语句?
I want to set a variable to use in a mysqli query. This doesn't quite work. Prior to mysqli I used to set query calls. I played around with db->multi_query($sql) with no luck. Anyone out there have an idea how to make this work including a set statement?
$sql = 'SET @rownum := 0;';
$sql .= 'SELECT @rownum :=@rownum + 1 AS Rank, User_Id, COUNT(User_ID) AS Block_Count
FROM Block_Owners;
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $world, $userId);
// execute the query
$stmt->execute();
推荐答案
在两个单独的查询中进行:
Do it in two separate queries:
$db->query('SET @rownum := 0');
$sql = 'SELECT @rownum :=@rownum + 1 AS Rank, User_Id, COUNT(User_ID) AS Block_Count FROM Block_Owners'
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $world, $userId);
$stmt->execute();
但是请注意,您要运行的查询将始终返回单行(Rank = 1),因为您使用的是没有 GROUP BY 的聚合函数.
Note, however, that the query you want to run will always return a single row (with Rank = 1) since you are using an aggregate function without GROUP BY.
这篇关于带有 SET 变量语句的 Mysqli 查询(即多个查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 SET 变量语句的 Mysqli 查询(即多个查询)
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
