Support » Fixing WordPress » Display mysql table data in a wp page

  • Resolved Agent047


    Hello, i am new to WordPress. My aim is to develop a standard website using wordpress(not a blog obviously). I use wordpress 3.3,mysql 5.5 and php 5.3. I need to display data from a table in mysql database on my wp page.I have created a table in the same database that wordpress was installed.(DBname-test,table_name-it_testtable: 2 columns).
    I had gone through the codex section and forums.I saw information on wpdb class and using the same with php.But i couldn’t quite figure out as to where i should write the php code to retrieve the data and display it in a page.
    Kindly help me with detailed info on how to retrieve data from the database table and display it on a page, say Table-data. If using php code, please specify the filename to insert the same.I am new to php as well, so please bear with me.Thanks a ton in advance.

Viewing 15 replies - 1 through 15 (of 25 total)
  • This sounds like it is too big a problem to be solved on this forum. You may need to hire someone to help. You can post a job request on

    Hello vtxyzzy,
    Thanks for your reply.Guess what, i got it solved! No need to hire anyone ;-).

    I can explain my solution here for the newbies who end up in this post, geeks, please pardon me if this was simple stuff for you… but then iam no geek… just a newbie.

    My aim was to display data from a mysql table onto a wordpress page. I could do it using wpdb class as mentioned in the codex.But i didn’t know the file where i should write this php code that contains wpdb class functions to retrieve data from mysql table.

    Hence i installed Exec-PHP plugin.(The Exec-PHP plugin executes PHP code in posts, pages and text widgets)

    Now you are free to write your php code in the page editor and get the retrieved data on your page.

    Steps to retrive data and display it on a wordpress page:
    Create a table in mysql database(assumed that you create a table in the wordpress database itself.) In my case database_name: test, table_name:it_testtable contains 2 columns: id(number(3)) and name(varchar).
    In wordpress, create a page and in the visual editor write the code(make sure you read the documentation of Exec-PHP plugin and do the required configuration mentioned,you will have to ‘disable the visual editor while writing’ option under Users section):

    Here is the code to retrieve data from it_testtable and display it on this page.(wpdb class is used for wordpress database,hence you need not mention the connection parameters as wpdb already has it configured to the ‘test’ database with the username and password):

    global $wpdb;
    /* wpdb class should not be called $wpdb variable is an instantiation of the class already set up to talk to the WordPress database */ 
    $result = $wpdb->get_results( "SELECT * FROM it_testtable "); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
    //echo "<pre>"; print_r($result); echo "</pre>";
    /* If you require you may print and view the contents of $result object */
    echo "ID"."  "."Name"."<br><br>";
    foreach($result as $row)
     echo $row->id."  ".$row->name."<br>";
     /* Print the contents of $result looping through each row returned in the result */

    Click on update button of the page created. Now you should view the created page in your website displaying the required table data.The data may not be displayed in a formatted manner.You may use html tables or other plugins to do the same. Hope this helps someone.

    Great code.Very simple but efficient.Thanks a lot.

    That is a nice option. A no code solution is WP Table Reloaded, a free plugin. It allows you to build a table in admin and then insert it into any page or post via a shortcode.



    hello i want to display data in my wordpress website page and i add data in one page and now this data should come automatically to another page how we can do it using database pls reply

    You could create a Page for the data and use a shortcode to display it in all the places you want it.



    Thanks but i made table where data inserted then after what code is to write and where in wordpress file and wordpress admin pls reply

    Or use a Desktop Publishing tool such as Windows Live Writer (where you can create tables), there are others.



    sorry but i m asking for code that what is to write in wordpress files and where in wordpress admin pages

    You do not need to know code to use WYSIWYG editors…and if you do, I am missing the point of this post…and, with WLW, you can just create pages if so desired.

    You should start a new post specific to your needs.



    i add jobs in one page and i want to insert into my database table and also in contact form(next page) so that what code is to write?

    It is not possible for us to guess what code needs to be written or where it needs to go.

    Here is a Codex article on interfacing with the database.

    The $wpdb object can be used to read data from any table in the WordPress database, not just the standard tables that WordPress creates.

    I suggest that you create a shortcode to read the table and format the data. Then, put the shortcode in all places where you want the data to appear.



    How to create footer in my wordpress theme

    Here is a Codex article on Theme Development. It includes a reference to a video tutorial.

    You should also find a theme with a structure similar to yours, but including a footer and examine its code.

    Normally, the footer code is in a file named footer.php and inserted into the theme with a call to get_footer().



    Hi i m using listing wordpress theme where i can add jobs name, category and so on but in database there are many tables and in one table jobs name and category column is available but still data is not present how to find the data of jobs name and category which in add in my site. pls reply

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Display mysql table data in a wp page’ is closed to new replies.