Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-03-02 23:48:44

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Executing SQL upon plugin install

How do you run an SQL query upon plugin install? The following works fine when my plugin is run from the cache, but the table isn’t created when the plugin is installed.

if (@txpinterface == 'admin') {
	$sql = "CREATE TABLE IF NOT EXISTS ".safe_pfx('jmd_rate')."(
		id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
		votes INT NOT NULL,
		value INT NOT NULL,
		used_ips LONGTEXT NULL
	);";
	safe_query($sql);
}

Offline

#2 2008-03-03 00:43:06

variaas
Plugin Author
From: Chicago
Registered: 2005-01-16
Posts: 402
Website

Re: Executing SQL upon plugin install

plugins aren’t loaded while you’re in the ‘plugin’ event. So, that SQL will run once you go to a backend page that’s not under the ‘plugin’ event.

Offline

#3 2008-03-03 02:30:01

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: Executing SQL upon plugin install

I wouldn’t recommend that snippet as-is: you’d be running the query on every page load.

Offline

#4 2008-03-03 02:39:58

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: Executing SQL upon plugin install

So what do you think I should do? I wasn’t planning on building an admin interface yet, but would that be the only way to create a table?

Offline

#5 2008-03-03 02:42:41

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: Executing SQL upon plugin install

Put the query inside a callback, then link to it from the plugin’s help doc, an “install” link.

Offline

Board footer

Powered by FluxBB