[resolved] i want to retrieve data from my database but it shows blank page (6 posts)

  1. killhunt888
    Posted 2 years ago #

    I have created a plugin about the social news that allows users to share their ideas by submitting a form ,it stores in DB but not showing the result it shows a blank page not any record
    please help
    thanx in advance ...

    //shows blank page
    function randomfnews() {
    global $wpdb;
    $fnews_table = $wpdb->prefix . 'fnews';
    $randomFact = $wpdb->get_results( "SELECT * FROM $fnews_table");
    $NumRows = count((array) $randomFact);
    //$RandNum = rand(0, $NumRows);
     $RandNum = rand(0, $NumRows-1); 
    print $randomFact[$RandNum]->name;
    print $randomFact[$RandNum]->news;
    //or return $randomFact[$RandNum]; ?
  2. Hi @killhunt888,

    I hope you are well today and thank you for your question.

    Is the data stored in fnews database table?

    Can you just write print_r($randomFact); in your code and check whether it displays any data or not?

    Best Regards,

  3. killhunt888
    Posted 2 years ago #

    Hi @WPMU DEV
    thanks alot for the responce. yes data stores successfully in wp_fnews table but it not retrieves & shows a blank page .

    i also tried print_r($randomFact); but still same situation .
    thanks again

  4. Hi @killhunt888,

    Thanks for reply.

    I meant if you write print_r($randomFact); then does it display any data on the page? Just copy paste here whatever you see there.

    Are you calling function randomfnews() somewhere? Just write echo 'function calld randomfnews'; in the function randomfnews and tell me is is displaying the message 'function calld randomfnews' ?

    Best Regards,

  5. killhunt888
    Posted 2 years ago #

    Thanks @WPMU DEV
    i got it actually i forgot to call function . I was confused to catch the error sometimes little mistakes causes big tension.
    now it displays random posts
    can you do an extra favor for displaying 7 new recent posts that made recently.
    thanks a lot

  6. Hi @killhunt888,

    Thanks for reply.

    You can use following code to display 7 new recent posts but i assume that you have post_date column in wp_fnews table which stores post creation date. if you don't have that column create one and add it's name in following query if it is different name.

    $randomFact = $wpdb->get_results( "SELECT TOP 7 FROM $fnews_table ORDER BY post_date DESC");

    Best Regards,

Topic Closed

This topic has been closed to new replies.

About this Topic