• Resolved weddellsea

    (@weddellsea)


    Not sure how to ask this; please be gentle. I have not been using WP for years. Just installed version 4.1.1.

    I have a pure HTML & PHP local tool that allows a user to query a video database (videodb) to search (read-only) for records in the database. I want to convert that tool to a wordpress plugin (I think plug-in is the proper choice). I intend to use the wpdb class to connect to the external database (same host as WP); I don’t perceive any issues with that.

    What I don’t know is how to structure user forms and then show results of the query. Would those be in WP Pages or Page Templates, or something else?

    There are eight queries (Title, Location, Genre, etc.). If a user wants to search for videos for an Actor a small form is presented where the user enters a string (actor’s name), submits the form which queries the database and lists the videos with that actor. The Forms are HTML but the rest of the logic is PHP, which I understand cannot go in WP Pages. Do I need two files per topic, one HTML form and one Page Template with PHP code? It’s how to organize this that has me confused.

    Suggestions are most welcome.
    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter weddellsea

    (@weddellsea)

    Is this post in the correct sub-forum, Plugins and hacks? Should it be moved to another place on this forum?

    I am trying the page template approach. Seems the right one. This WP site is on a server only accessible on my home LAN.

    But I am not sure accessing the external database (via wpdb class) is working for me. Suggestions on how to test that wpdb class usage is/is not working please. See what I have tried so far —

    <?php
    /**
     * Template Name: Page qryVids Test
     * Description: Test access to the videodb database
     * This is a page template file to reside in the Theme's page templates
     *
     * Created: March 2015 -- Jim
     * HISTORY:
     *   12 Mar 2015 -- created for use within WP
     *
     *
     */
    
    get_header(); ?>
    
    <div id="primary" class="site-content">
    	<div id="content" role="main">
    
    <!-- ----------  Begin my custom WP Page Template code  ------------- -->
    		<p>This is HTML text</p>
    
    <?php
    	echo ("<p>this is PHP test text</p>");			
    
        echo "<p>qryVids database: " . get_option('qryvids_dbname') . "</p>";
    	$viddb = new wpdb(get_option('qryvids_dbuser'),get_option('qryvids_dbpwd'), get_option('qryvids_dbname'), get_option('qryvids_dbhost'));
    	$viddb->show_errors();
    
    		$videocnt = $viddb->get_var("SELECT count(*) FROM videodata");
    		echo "<p>Number of videos in the database = $videocnt </p>";
    
            echo "<p>Last Query: $viddb->last_query </p>";
    		echo "<p>Last Error = $viddb->last_error </p>";
    
    ?>
    <br>
    <br>
    <br>
    <br>
    <br>
    
    <br>
    <br>
    <br>
    
    <!-- ----------  End of my custom code  ----------------- -->
    	</div> <!-- end of #content -->
    </div> <!-- end of #primary -->
    <?php get_footer(); ?>

    The output on the WP page is:

    This is HTML text

    this is PHP test text

    qryVids database: videodb

    Number of videos in the database =

    Last Query:

    Last Error =

    There should be 921 records reported in the Select count(*), why nothing?
    No query shows in the last_query varable, why?
    I have no confidence there are no errors, but at least WP doesn’t think there are any.

    Thread Starter weddellsea

    (@weddellsea)

    Database access issue solved! The MySQL user I was using seems to need some additional permissions for the wpdb class; haven’t figured out what yet. But accessing the external database using my full-permission root account works. Outside WP that other MySQLl user credentials work.

    Custom page template approach is working for me. I will mark this resolved.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to Create Plugin for Queries to External Database’ is closed to new replies.