Latest Register Log In

+ Advanced Search

Running Arbitrary SQL or PHP

Running Arbitrary SQL or PHP
By
03/22/14

Sometimes you may need to run some SQL queries or PHP code on your site. Admin Panel -> Miscellaneous -> Advanced Options enables this. Of course, you must know SQL/PHP or be copying from a source that knows in order to do this.

Running SQL Queries

In the "Run SQL on Database" box you can type/paste any queries you need to run. These queries will run in WSN's database as the mysql user specified during setup. You can use {PREFIX} as a stand-in for WSN's database tables prefix for the current installation, in case you don't remember what prefix you're using. For example, to run a query to reset all views counters to zero for listings added in the last 24 hours you would do
UPDATE {PREFIX}links SET hits=0 WHERE time > UNIX_TIMESTAMP() - 60*60*24;

When running multiple queries, be sure to put one query per line with a semicolon terminating each query. Note that SELECT queries will be useless -- they'll run but nothing will be output. To retrieve data you'd need to run PHP which does the query and then displays the results.

You also have the option to upload a .sql file containing a series of queries. Uploading large SQL files is not recommended as they may timeout or hit memory limits. It's better to use PHPMyAdmin for uploading SQL files, as PHPMyAdmin has better tools for loading them in parts.

Running PHP

In the "Execute PHP Code" box you can type/paste any PHP code you need to run. The opening <?php and closing ?> are optional. The code executes inside a special function, so in order to get access to any WSN variables you'll need to globalize them. For example, if you want to manipulate $settings, write global $settings; before you reference $settings. If you're doing any filesystem operations, keep in mind that the script is running from the admin panel so you need a ../ to go up to WSN's base directory.

You can also upload a .php file, but keep in mind the file will have to be written to run from the admin panel directory path.





Rating
Views 261 views. Averaging 0 views per day.