Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1015369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:23:29+00:00 2026-05-16T10:23:29+00:00

I’ve got an sql query that contains a number of statements. It: sets a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:23:30+00:00Added an answer on May 16, 2026 at 10:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.