Page 1 of 1

mysql table naming

Posted: Tue Oct 03, 2006 10:37 am
by KSquared
Hi all,

Im wondering, can I have a table name with 2 words, ie. 'my table'?

Heres what I have:

Code: Select all

$myTable = "'".$row['prod_name']."'";
$dbquery->buildSelectQuery("*", $myTable, false,'');
The actual name of the table is 'Arabidopsis Chambers'. If I do not enclose the table name with ' ' , it throws a "no table Arabidopsis" exists. So I see it leaves off the second word "Chambers".

If I enclose it with ' ' , it then throws:
"Check the manual that corresponds to your MySQL server version for the right syntax to use near ''Arabidopsis Chambers'' at line 1".

Here is the function from my DbQuery class:

Code: Select all

function buildSelectQuery($selectWhat, $table, $where, $whereParams)
	{
	// $selectWhat = What to select, certain fields or *
	// $table = What table to select from
	// $where = Check if it needs a WHERE clause - true or false
	// $whereParams = What are the paramaters
		
		// check if a WHERE statement is needed
		if($where){
			
			$this->queryString = "SELECT ".$selectWhat." FROM ".$table." WHERE ".$whereParams;
		}else{
		
			$this->queryString = "SELECT ".$selectWhat." FROM ".$table;
			
		}
		$this->result = mysql_query($this->queryString) or die("Could not connect to MySQL database from buildSelectQuery" .mysql_error());
	}
Any suggestions would be great.
Thanks
Keith

Posted: Tue Oct 03, 2006 10:40 am
by Burrito
you can have a table with spaces in the name (though I wouldnt' recommend it). you need to enclose the table name in backticks (`). That's the same key as your tilde key.

Posted: Tue Oct 03, 2006 10:56 am
by KSquared
ah backtik... thank you.

Could you elaborate a bit on why it would be a bad idea for a 2 word table name? just curious...

Posted: Tue Oct 03, 2006 11:05 am
by Burrito
KSquared wrote:Could you elaborate a bit on why it would be a bad idea for a 2 word table name? just curious...
by convention multiple word table names have underscores to separate the words. A reason why it's a bad idea to have two separate words...how about the reason you posted this thread :)

Posted: Tue Oct 03, 2006 11:07 am
by KSquared
lol... good point!

Thanks Burrito.