从 msqli::multi_query 取回行时遇到问题

发布于 2024-09-14 15:52:50 字数 1202 浏览 7 评论 0原文

我有一个包含许多语句的 SQL 查询。它:

  1. 设置一个用户变量
  2. 调用一个存储过程
  3. 调用另一个存储过程
  4. 选择一些数据

我知道查询是正确的,因为我已经在 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:

  1. sets a user variable
  2. calls a stored procedure
  3. calls another stored procedure
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢你一世 2024-09-21 15:52:50

试试这个:

$query = new MySQLi(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
if ($query->multi_query($sql)) {
    do {
        if ($result = $query->use_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
    } while ($query->next_result());
} else {
   echo 'ERROR FOR QUERY!';
}

这应该可以帮助您捕获任何错误。另外,我认为您的 use_result 需要与 next_result 交换。

更新:另一件事,您是否检查过以确保传递给查询的变量实际上包含数据?打印我们的查询以确保您可以在数据库中运行它并手动获取结果。

Try this:

$query = new MySQLi(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
if ($query->multi_query($sql)) {
    do {
        if ($result = $query->use_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
    } while ($query->next_result());
} else {
   echo 'ERROR FOR QUERY!';
}

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.

且行且努力 2024-09-21 15:52:50

您显然没有从结果中获取行。请将代码更改为此,它将打印结果。

$query = new MySQLi(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query->multi_query($sql);

do {
       /* store first result set */
       if ($result = $query->use_result())
       {
           while ($row = $result->fetch_row())
           {
               print_r($row);
           }
           $result->close();
       }
       /* print divider */
       if ($query->more_results())
       {
           printf("-----------------\n");
       }
   } while ($query->next_result());

You are apparently not fetching the rows from the result. Please change the code to this and it will print the results.

$query = new MySQLi(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query->multi_query($sql);

do {
       /* store first result set */
       if ($result = $query->use_result())
       {
           while ($row = $result->fetch_row())
           {
               print_r($row);
           }
           $result->close();
       }
       /* print divider */
       if ($query->more_results())
       {
           printf("-----------------\n");
       }
   } while ($query->next_result());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文