从 msqli::multi_query 取回行时遇到问题
我有一个包含许多语句的 SQL 查询。它:
- 设置一个用户变量
- 调用一个存储过程
- 调用另一个存储过程
- 选择一些数据
我知道查询是正确的,因为我已经在 MySQL Workbench 中在同一用户下测试了它。查询是这样的:
set @current_post = 535; /* 535 for testing, this will be a variable */
call under_user_type(@currrent_post, @user_type, @user_id);
call get_category(@current_post, @category);
select p.post_title, p.post_name,
(
swell_wp.post_score(p.ID)
) as score,
(
swell_wp.is_under_user(p.ID, @user_type, @user_id)
) as under_user,
(
swell_wp.is_under_category(p.ID, @category)
) as under_category
from wp_posts as p
where p.post_type = 'post'
and p.id != @current_post
and p.post_status = 'publish'
having (
under_user = true
or under_category = true
)
order by score desc;
它只是存储在字符串中:$sql
。然后我在 PHP 中使用它来执行此操作:
$query = new MySQLi(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query->multi_query($sql);
do {
$query->next_result();
} while( ! $result = $query->use_result() );
print_r($result);
这将打印一个结果对象,但该对象是空的。尝试迭代它也不会产生任何结果。
我做错了什么?我什至可以使用这样的用户变量吗?或者我需要将过程转换为存储函数并执行三个单独的查询?
I've got an sql query that contains a number of statements. It:
- sets a user variable
- calls a stored procedure
- calls another stored procedure
- selects some data
I know that the query is correct, because I've tested it in MySQL Workbench, under the same user. The query is this:
set @current_post = 535; /* 535 for testing, this will be a variable */
call under_user_type(@currrent_post, @user_type, @user_id);
call get_category(@current_post, @category);
select p.post_title, p.post_name,
(
swell_wp.post_score(p.ID)
) as score,
(
swell_wp.is_under_user(p.ID, @user_type, @user_id)
) as under_user,
(
swell_wp.is_under_category(p.ID, @category)
) as under_category
from wp_posts as p
where p.post_type = 'post'
and p.id != @current_post
and p.post_status = 'publish'
having (
under_user = true
or under_category = true
)
order by score desc;
that's just stored in a string: $sql
. I then do this with it in PHP:
$query = new MySQLi(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query->multi_query($sql);
do {
$query->next_result();
} while( ! $result = $query->use_result() );
print_r($result);
this prints a result object, but one that is empty. Trying to iterate over it doesn't produce any results either.
What am I doing wrong? Can I even use user variables like this? Or will I need to turn the procedures into stored functions and do three separate queries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
这应该可以帮助您捕获任何错误。另外,我认为您的 use_result 需要与 next_result 交换。
更新:另一件事,您是否检查过以确保传递给查询的变量实际上包含数据?打印我们的查询以确保您可以在数据库中运行它并手动获取结果。
Try this:
This should help you trap any errors. Also, I think your use_result needs to be swapped with the next_result.
UPDATED: One other thing, have you checks to make sure the variables you are passing to the query actually contain data? Print our the query to make sure you can run it in the database and get results manually too.
您显然没有从结果中获取行。请将代码更改为此,它将打印结果。
You are apparently not fetching the rows from the result. Please change the code to this and it will print the results.