Support » Plugins and Hacks » Issue while fetching data from wordpress database custom table

  • Resolved annujames

    (@annujames)


    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • What about using get_results instead of get_row?

    See wpdb

    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;
    }
    }

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issue while fetching data from wordpress database custom table’ is closed to new replies.