WordPress.org

Ready to get started?Download WordPress

Forums

WordPress and MySQL Database integration (10 posts)

  1. jlee_shiver
    Member
    Posted 3 months ago #

    Hi!

    I have an existing database (MySQL using phpMyAdmin) that I would like to display information from on my WordPress site.

    When I installed WordPress, I created a new database in phpMyAdmin for WordPress, which was populated automatically (as per the installation).

    How do I/is there a way to integrate the database I've built (this is for work) into my WordPress site?

    I'm a beginner at this sort of stuff so any help would be very much appreciated! Thank you!

  2. bcworkz
    Member
    Posted 3 months ago #

    I don't think "integrate" is the right term. You can access the data using PHP functions like mysqli_query() but any of the usual WP functions to access the database, including $wpdb methods, will not work because such data needs to be in the WP database.

    You thus need to be responsible for the connection details yourself that are usually done behind the scene by WP functions, which is why I say "integrated" is not the right term. Regardless of what I call it, you can access the data.

  3. jlee_shiver
    Member
    Posted 3 months ago #

    Okay, thanks.

    I understand how to connect to a database using PHP.

    My ultimate goal is to be able to run queries and display them using my WordPress site.

    Would I need to create a widget for that?

  4. catacaustic
    Member
    Posted 3 months ago #

    That all depends on how you want to display the data.

    There's a lot more options then just widgets. I'd also suggest looking at shortcodes, and even creating custom post types to handle the data if it's going to be large. Unfortunatly without knowing what the data is and how you want it displayed it's almost impossible to give any concrete ideas.

  5. jlee_shiver
    Member
    Posted 2 months ago #

    It's performance of mobile games across time. So, top charts. For example, I want to be able to input "Game X" and get it's average revenue, number on the best selling chart, etc. I have the database set up, I'm just in need of an elegant way to display my data.

    If you have any ideas I would be immensely grateful!

  6. bcworkz
    Member
    Posted 2 months ago #

    Besides widgets and shortcodes, the other option for displaying data is custom templates. A custom template can be used for displaying custom post types, or you can create a page template for the basis of creating pages to display your data. By "page" I mean the WP post_type page, not any old generic webpage page.

    Widgets are good for supplementary information shown in sidebars and footers or other such designated areas. Shortcodes are good for inserting data into regular post content. Custom post types are more of an organizational tool than a way to display data, but templates can easily be associated with them, which are exactly for displaying data.

    Page templates are good for displaying data in a particular way as a complete page in of itself, contrasted with the typical WP index page that shows any number of various posts. Still, page templates can be made to do that as well. Templates are very flexible, but are not suited for inserting into post content.

    From your brief description, I think a custom post type with a single page template is the way to go. Each game can be a post. All games can be listed on an archive page just like other posts would be. The single page can show the charts and all associated data in a single, nicely organized page for one particular game, much like finance data for a particular stock.

  7. jlee_shiver
    Member
    Posted 2 months ago #

    @bcworks thank you for your answer! This is super duper helpful!

  8. jlee_shiver
    Member
    Posted 2 months ago #

    Displaying data from another database using PHP scripts to run SQL queries on said database.

    Creating a custom page (not post?) type to do this.

    Do I just input my PHP script into the default page.php file and save it as a new .php file?

  9. catacaustic
    Member
    Posted 2 months ago #

    In WordPress, everything is a "post", so pages are just another post type. That's why we say custom post types as opposed to custom page types. They are really the same thing when it comes down to it.

    Creating a custom post type is more work then creating a template. It does have the advantage of keeping things separate in your admin are though, which may or may not be useful to you. If you go the custom post type route then you'd set up the custom post type in your funtions.php file, and create seperate template files for that type.

    You can just create a custom page template that will drag in the information if that's what you're after as that will also work, but that will leave everything mixed in with your other pages.

  10. bcworkz
    Member
    Posted 2 months ago #

    Sorry for the late reply, I've been away.

    catacaustic quite capably explained your options, I just wanted to add more specificity related to template filenames.

    If you choose not to use a custom post type, a page template can be named anything, the name shown in the dropdown template selection on the page editor screen comes from the header information. It can be based on page.php if you like, but you need to add something like Template Name: Games to the header.

    OTOH, the filenames for templates for a custom post type is specific. They must be of the form single-{$post_type}.php and archive-{$post_type}.php, so if your CPT is "games", then the template filenames are single-games.php and archive-games.php.

Reply

You must log in to post.

About this Topic