• On the same website where I host my WordPress site, I have a separate custom MySQL database that has some race results. I have a PHP page that runs a query on the MySQL database, and returns the results as a table. It all works fine.

    What I would LIKE to do is incorporate this into my WordPress site so that the PHP form and resulting table have the same theme settings as my site.

    I have taken a copy of my themes PAGE.PHP file and renamed it to TESTPAGE.PHP – I have added the Template name to it (My Test Template). I have also added all of the PHP script from my working file.

    I have created a new page within WordPress, and from the templates dropdown, I have chose My Test Template.

    Now, when I load the page, my form appears as expected. I enter the search criteria and press the submit button.

    When the page reloads (with the parsed parameters) I can see that my MySQL query looks ok because I am echoing it to the page, but the query doesn’t return any results. As I mentioned above, this all works as a standalone PHP file, so I wonder if there is something else that is stopping the query from running?

Viewing 4 replies - 1 through 4 (of 4 total)
  • It sounds like you are on the right track.

    If you’re not getting any PHP errors, there are a couple of things you can check.

    First, try echoing your parsed parameters to make sure they are getting through to your script and are thus included in the query.

    Check the way you access your database. If you’re using a different database than WordPress, you’ll need to make sure your connection is set up properly. From the Codex:

    The $wpdb object can talk to any number of tables, but only to one database at a time; by default the WordPress database. In the rare case you need to connect to another database, you will need to instantiate your own object from the wpdb class with your own database connection information.

    Thread Starter chrismillard

    (@chrismillard)

    I’m already echoing the full query and it looks OK. However, I haven’t been using the $wpdb object at all – I have never used it so will have to read up on it. I assume that you HAVE to use the $wpdb object?

    I didn’t think your query would show the variables since you are presumably using PDO and binding parameters for security reasons.

    No, I don’t think you have to use the $wpdb object. I don’t see why you couldn’t use PDO instead. I was just thinking that you might as well use WordPress functions since you’re in there, though using a different database it may be more hassle than it’s worth.

    Thread Starter chrismillard

    (@chrismillard)

    Tried using $wpdb and it is working perfectly now.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting data from custom MySQL table’ is closed to new replies.