Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I've Googled and followed countless tutorials, read documentation on MySQL, Visual Studio, WordPress, and Plesk. I try to leave posting on this site as the final option and it seems it has come to that.

As simply put as possible, I want to display the list of categories from my WordPress blog on another ASP.NET/C# website that I have. There isn't an RSS of the names of the categories specifically otherwise I would have used the XML from the RSS feed with XSLT to display the category list that way. I came to the conclusion that I had to access my WordPress database (where the category names are stored) directly from my other website.

To do so, I've downloaded and installed the MySQL Connector Net 6.5.4 on my localmachine (C: Drive) allowing the "MySQL Database" option to be available when adding a Data Connection in Visual Studio. I also, downloaded the 'noinstall' of the MySQL Connector Net 6.5.4 by selecting ".NET & MONO" from the "Select Platform:" dropdown on MySql.com in order to add a reference to the DLL in my C# code.

I get the same error when either attempting to run the code or adding a data connection. Adding the data connection was originally to test the actual connection to the database after I got the error from running the code.

Error: "Unable to connect to any of the specified MySQL hosts."

Below is the code I have in my 'Default.aspx.cs' file:

...
using MySql.Data.MySqlClient;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
...
        String MyConnectionString = "SERVER=localhost;UID=testusername;PASSWORD=testpassword;DATABASE=testdbname;";
        MySqlConnection connection = new MySqlConnection(MyConnectionString);
        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "SELECT * FROM wp_terms wt " +
                      "INNER JOIN wp_term_taxonomy wtt ON wt.term_id=wtt.term_id " +
                      "WHERE wtt.taxonomy='category' ORDER BY wt.name";
        try
        {
            connection.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        MySqlDataReader Reader = command.ExecuteReader();
        while (Reader.Read())
        {
            string thisrow = "";
            for (int i = 0; i < Reader.FieldCount; i++)
            {
                thisrow += Reader.GetValue(i).ToString() + ",";
                Label1.Text += thisrow;
            }
        }
        connection.Close();
    }
}

At this point, I am convinced that the problem lies with the credentials that I am entering in String MyConnectionString = "SERVER=localhost;UID=testusername;PASSWORD=testpassword;DATABASE=testdbname;"; or when attempting to 'Add a Data Connection'. However, I checked the wp-config.php file within my WordPress installation in the blog's root directory and am, therefore, positive that I am providing the correct credentials. The only one I am slightly unsure of is the Database server name [localhost].

Is there something else I am not doing correctly? Is it possible the WordPress databases don't allow for remote access?

Any help would be greatly appreciated! Thank you in advance!

P.S. I've posted this question on Stack Overflow as well.

share|improve this question
    
Have you tried specifying the port? By default, MySQL listens on port 3306. So give SERVER=localhost;PORT=3306;UID=testusername;PASSWORD=testpassword;DATABASE=test‌​dbname; a try for your connection string. –  EAMann Apr 16 '12 at 15:54
    
I have. I have tried several variants of the connection string. No luck. Same error message. –  JSco Apr 17 '12 at 0:40
add comment

1 Answer 1

up vote 1 down vote accepted

There isn't an RSS of the names of the categories specifically otherwise I would have used the XML from the RSS feed with XSLT to display the category list that way.

There is a handy function named add_feed(). You can create any feed or other output with it. Should be useful in your case. And it is pretty simple too.

Sample plugin

<?php
/**
 * Plugin Name: Category Feed
 * Description: XML output for the categories located on /catxml/
 */

add_action( 'init', array ( 'T5_Cat_Feed', 'init' ) );

class T5_Cat_Feed
{
    public static function init()
    {
        add_feed( 'catxml', array ( __CLASS__, 'display' ) );
    }

    public static function display()
    {
        $args = array(
            'hide_empty'               => 0,
            'hierarchical'             => 0,
            'taxonomy'                 => 'category'
        );
        $cats = get_categories( $args );

        if ( empty ( $cats ) or ! is_array( $cats ) )
        {
            return;
        }

        header( 'Content-Type: text/xml' );
        print '<categories>';

        foreach ( $cats as $cat )
        {
            $url = get_category_link( $cat->term_id );
            print "<category>
<name>$cat->cat_name</name>
<id>$cat->term_id</id>
<url>$url</url>
<description>$cat->category_description</description>
<parent>$cat->parent</parent>
<count>$cat->count</count>
</category>"
            ;
        }
        print '</categories>';
    }
}

Install as a plugin on your blog, visit the permalink settings page once to refresh the rewrite cache, and go to example.com/catxml/ or example.com/?feed=catxml. You get a simple XML file with category data here.

share|improve this answer
    
Wow, this is great! Thanks toscho. However, I am new to WordPress development. This code isn't something I should add into my functions.php file? I should add it as a plugin? How do I do that? –  JSco Apr 15 '12 at 19:27
    
Save the code as something like categoryfeed.php and then upload the file via the Upload page in the Add New Plugin section of my Dashboard? –  JSco Apr 15 '12 at 19:47
    
@JSco Yes, exactly. –  toscho Apr 15 '12 at 22:23
    
If I may inquire about some of the specifics in the code you provided for the plugin, what is the significance of... (1)"T5" in the class title? (2)"CLASS" in the first parameter of the array in the add_feed function? Am I correct in assuming I can use the code you provided exactly as is, without making any changes? –  JSco Apr 16 '12 at 3:10
    
T5 is my prefix, because I prefix everything. __CLASS__ is a magic constant with the current classes name. Callbacks which are static methods need the class name as the first parameter. The plugin is running as is, see this example. –  toscho Apr 16 '12 at 3:19
show 11 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.