Categories
PHP

Test for Database Table in PHP

Sometimes it's handy to be able to detect whether a given table exists, such as when you want to create a table if it isn't present.

The following example will detect the presence of a table in the simplest way I can think of.

 if(mysql_num_rows( mysql_query("SHOW TABLES LIKE 'tablename'"))) {
    // table is present
    // execute some code here
 };

Alternatively, a small change to reverse the logic will let you react if the table does not exist. Note the exclamation point which means "not" and changes the logic to respond if the table does not exist.

 if(!mysql_num_rows( mysql_query("SHOW TABLES LIKE 'tablename'"))) {
    // table is not present
    // here you might want to create the missing table
 };

If you're not familiar with PHP, the logic of the first if() statement says essentially this:

Query the database to see how many tables are named 'tablename'.

Because only one table in the database can have the name, only two responses are possible: 1 or 0. In boolean logic, those values represent Yes/No or True/False respectively and can be acted on.

If the condition is satisfied (yes, the table exists in the first example above or no, the table does not exist in the second example above), the code between the braces {} will be executed.

Categories
MySQL

Trim leading whitespace from a field in MySQL

As always, backup the database or at least the affected table before running a command that will change every record in a table as this does.

The following line will remove the leading spaces (note the parameter '\t'). For new lines use '\n' and for carriage returns use '\r'.

UPDATE members SET memberid = REPLACE(memberid,'\t','');

For more information, see http://stackoverflow.com/questions/281724/does-the-mysql-trim-function-not-trim-line-breaks-or-carriage-returns.

Categories
MySQL

Using Variables in MySQL

Finding the correct syntax for using variables in MySQL can be surprisingly difficult.
Once you know it, it's easy. Here's a start:

SET @username := 'jonesr';
SELECT id,fname,lname FROM staff WHERE username = @username;
Categories
Web Development

Subversion Notes

As I dip my toe in the SVN (Subversion) pond, I'll post here some resources for my own edification and that of anyone else who cares to follow this page.

Subversion Book Version Control with Subversion: For Subversion 1.5

Getting Started with SVN on Snipe.net

Categories
Joomla!

Rokbridge Install Error

Rokbridge is a Joomla!/phpbb bridge designed to allow the use of Joomla authentication on a phpbb install.

Upon install, running Joomla 1.5.14, The following error was encountered when I accessed rockbridge through the Joomla! admin panel:

Fatal error: Class 'JFile' not found in /home/…/administrator/components/com_rokbridge/helper.php on line 145

This was a little difficult to track down, (more reports than solutions) but the solution seems to be to add the following line immediately prior to line 145 in the referenced file;

[sourcecode language="php"]
jimport( 'joomla.filesystem.file' );
[/sourcecode]

In my case, I added a comment and a line of whitespace above and below for clarity, so I have the following code: starting at line 130 in helper.php:

[sourcecode language="php"]

function getParams($refresh = false)

{

static $instance;

 

if ($instance == null || $refresh)

{

$component="com_rokbridge";

 

$table =& JTable::getInstance('component');

$table->loadByOption( $component );

 

// work out file path

$option = preg_replace( '#\W#', ", $table->option );

$path = JPATH_ADMINISTRATOR.DS.'components'.DS.$option.DS.'config.xml';

 

// following line added to fix error at install – see http://www.rockettheme.com/forum/index.php?f=199&t=115248&rb_v=viewtopic

jimport( 'joomla.filesystem.file' );

 

if (JFile::exists( $path )) {

$instance = new JParameter( $table->params, $path );

} else {

$instance = new JParameter( $table->params );

}

}

 

return $instance;

}

[/sourcecode]

 

Reference: http://www.rockettheme.com/forum/index.php?f=199&t=115248&rb_v=viewtopic