WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Issue while fetching data from wordpress database custom table (4 posts)

  1. annujames
    Member
    Posted 5 years ago #

    Hi,

    I am new with the wordpress. I do have some knowledge about php and mysql. I am trying to create a simple custom function using custom table in wordpress database. Its a very simple function. I am creating custom function in function.php. It is giving me output However I am unable to figure it out why it is giving only single record in output. It might be due to array, but I am week in using array in php.

    Description about function: I have created one table in wordpress database and trying to retrieve number of records from that table (more than one record).

    Here is the function I created.

    function my_posts()
    {
    global $wpdb;

    $checkpost = $wpdb->get_row("SELECT record_content FROM wp_my_posts WHERE my_ID = '15'");

    foreach($checkpost as $mypost_content)
    echo $mypost_content;

    }

    Here my_ID is not unique it has multiple records and I want to show those multiple records as a output of a query. Currently is showing up only first record in the output.

    Can any one help me out here.

  2. MichaelH
    Member
    Posted 5 years ago #

    What about using get_results instead of get_row?

    See wpdb

  3. annujames
    Member
    Posted 5 years ago #

    I used get_results aswell, however, in output it is showing up the same single result, where as the output is 3 records. If you see following code the result for post_count is coming up 3 but for mypost it showing up single record, how do I print all the 3 results?

    function my_posts()
    {
    global $wpdb;
    $checkpost = $wpdb->get_results("SELECT record_content as mypost, COUNT( record_id ) as post_count FROM wp_my_posts WHERE my_ID ='15'" );

    foreach($checkpost as $checkposts)
    {echo $checkposts->post_count;
    echo $checkposts->mypost;
    }
    }

  4. annujames
    Member
    Posted 5 years ago #

    Hi Michael,

    Thanks for guiding me to the rite direction, I went to wpdb page and found a solution. It has explaned everything.

    Thanks,
    James

Topic Closed

This topic has been closed to new replies.

About this Topic