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 7058877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:09:54+00:00 2026-05-28T04:09:54+00:00

I have tried several things but I can’t get the MySQL query in my

  • 0

I have tried several things but I can’t get the MySQL query in my php code to work. Note that the query DOES WORK when I hard-code the variable $lastmsg which is an integer.

The code works in two instances:

  1. When the page loads, the query runs, ignoring $pagination because $lastmsg is not set.
  2. A button sends a value for lastmsg to this page, which runs the code again, this time looking at $pagination because lastmsg is set.

Again, the query DOES WORK. The proof being that it runs perfectly fine in the first instance and returns properly when the variables are hard coded. Firebugs reports that lastmsg is being sent by post.

The problem is that the query isn’t receiving the value for $lastmsg (I get no values returned). I know this is because $lastmsg is sitting inside $pagination.

P.S. I am aware it needs injection protection… I’m just trying to get this to work:

if(isset($_POST['lastmsg']) &&is_numeric($_POST['lastmsg'])){
    $lastmsg = $_POST['lastmsg'];
    $pagination = 'AND $wpdb->posts.ID < ".$lastmsg."';
} else {
    $pagination = '';
}

$pageposts = $wpdb->get_results($wpdb->prepare("
    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'post' 
    AND $wpdb->posts.post_date < NOW()+INTERVAL 1 DAY 
    AND $wpdb->term_taxonomy.taxonomy = 'category' 
    AND $wpdb->term_taxonomy.term_id IN(3)
    ".$pagination."
    ORDER BY $wpdb->posts.post_date DESC
    LIMIT 10
    "), OBJECT); 

The following DOES NOT work

$pagination = 'AND $wpdb->posts.ID < ';
$pageposts = $wpdb->get_results($wpdb->prepare("
    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'post' 
    AND $wpdb->posts.post_date < NOW()+INTERVAL 1 DAY 
    AND $wpdb->term_taxonomy.taxonomy = 'category' 
    AND $wpdb->term_taxonomy.term_id IN(3)
    ".$pagination."
    ".$lastmsg."
    ORDER BY $wpdb->posts.post_date DESC
    LIMIT 10
    "), OBJECT); 

Any solutions?

  • 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-28T04:09:54+00:00Added an answer on May 28, 2026 at 4:09 am

    This is not the string you’re looking for:

    $pagination = 'AND $wpdb->posts.ID < ".$lastmsg."';
    

    That will leave a literal AND $wpdb->posts.ID < ".$lastmsg." inside $pagination and that string won’t do anything useful when you hand it to MySQL.

    You want double quotes on the outside (for interpolation) and no single quotes at all:

    $pagination = "AND $wpdb->posts.ID < $lastmsg";
    

    Or, if $lastmsg is a string rather than a number:

    $pagination = "AND $wpdb->posts.ID < '$lastmsg'";
    

    That should get your first version working.

    Now go forth and add all your mysql_real_escape_string calls.

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

Sidebar

Related Questions

I've tried several things, but none of them work... I have Form that should
Here is what I have. I have tried several things but I can't figure
Can someone convert this english to SQL I have tried several things but no
I have tried several things so far, but I haven't had much luck yet.
I have tried several things like archflags, mysql-config directory settings and everything. I am
I have tried several things to get QScrollBar's slider to change size based on
I have a question concerning seekbars. I tried several things but nothing helped. I
I have tried quite a few things but I still don't know how can
I have tried several of the solutions I have seen out here, but am
I have tried several mySQL queries to compare a column of dates stored as

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.