Help - Search - Members - Calendar
Full Version: Mtsql Plugin And Random Quote Table
Movable Type Community Forum > Additional Resources > Plugin Development and Usage
lkelly
I have a table in the database which stores quotes. I want to pull a random quote from this table and display it on the page. I know how to do all this with php, but I'm not sure how to convert all my templates into php files and have everything work. The fallback is the MTSQL plugin.

Here's my table structure (very simple):

id quote

Using the MTSQL plugin, here's the code:

CODE
<MTSQL query="SELECT id, quote, RAND() as r FROM quotes ORDER BY r LIMIT 1"><MTSQLColumn column='2'></MTSQL>


Now I can run the query in phpMyAdmin and I get a random record from the table in the results pane. I'm reasonably confident that it's correct from a SQL perspective. However, when I use the above code on my page, I only seem to get the first quote in the table each time. No amount of refreshing the page gives me another quote.

So, a few questions:

1. Is my syntax with the MTSQL plugin correct?
2. Do I need to close the <MTSQLColumn> tag? I think I threw in a </MTSQLColumn> before </MTSQL> but the actual text of the tag showed up in the resulting page source when viewed in the browser.

Any help? Thanks.
jayseae
I think the problem is the way in which you are using the plugin. The MTSQL code executes when you build the page, not when the page is displayed. The page is static, unlike PHP, which is dynamic, executing the query each time the page is displayed. If you wanted to do this on a daily basis, you could rebuild the page every day, either manually or via CRON or something.

Alternatively, switch your pages to PHP and use EZ SQL (or build your own PHP access to the database), which will allow the quote to refresh every time that the page is displayed to a browser.
lkelly
Now that I've done a bit more testing, I will see other quotes on different pages where I've added this code. It makes me wonder about the way this plugin works. Is it truly like php where it will execute the code each time the page is loaded and pull content from the database? Or is it actually executed when the page is built from within the template section of the configuration UI and the data (quote) is added to the index.htm right at that point? Meaning, it is randomly adding quotes during rebuild time, but not during view refresh time.

Thanks.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.