1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Wordpress wpdb->prepare strange error

Discussion in 'PHP' started by irfan saleem, Jun 1, 2014.

  1. #1
    I am trying to use wordpress $wpdb in this way:

        function check_date($date) {
       
        global $wpdb;
    
        $pub = "publish";
    
         $query = $wpdb->prepare("SELECT post_date FROM " . $wpdb->posts . " WHERE post_name = %s AND post_status = %s;", sanitize_title_with_dashes($result[$i]->title), $pub);
            $cID = $wpdb->get_row( $query );
    
         echo "". date("Y/m/d", strtotime($cID->post_date)) . " == ".$date. "<br />";
       
        return (date("Y/m/d", strtotime($cID->post_date)) == $date);
       
        }
    
    Code (markup):
    The output of echo statement should be:
    but instead it shows:
    and when i change the code to include only 1 $arg it works in this way i.e.

         $query = $wpdb->prepare("SELECT post_date FROM " . $wpdb->posts . " WHERE post_name = %s;", sanitize_title_with_dashes($result[$i]->title));
    Code (markup):
    It displays the output as under:
    which is not correct as it is the date of a draft or revision of the post i am trying to get the date of.

    Any help will be appreciated.

    Thanks.
     
    irfan saleem, Jun 1, 2014 IP
  2. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #2
    It seems your query returns NULL. Have you checked what errors WordPress generates?

    $wpdb->show_errors();
    Code (markup):
    $wpdb->print_error();
    Code (markup):
    And I don't see a reason why you should should prepare the $pub variable. If it's not a user generated input, you can use it directly in your query:

    
    $query = $wpdb->prepare( "SELECT post_date FROM $wpdb->posts WHERE post_name = %s AND post_status = '$pub'", sanitize_title_with_dashes( $result[$i]->title ) );
    
    Code (markup):
    Tip: If you only select one field in your query (post_date) you can use $wpdb->get_var instead of $wpdb->get_results. It returns a single variable instead of the object.

    Anyway, why do you use both echo and return in your function?
     
    bogi, Jun 2, 2014 IP