mysql table naming

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
KSquared
Forum Commoner
Posts: 25
Joined: Tue Aug 29, 2006 4:07 pm

mysql table naming

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
KSquared
Forum Commoner
Posts: 25
Joined: Tue Aug 29, 2006 4:07 pm

Post 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...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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 :)
KSquared
Forum Commoner
Posts: 25
Joined: Tue Aug 29, 2006 4:07 pm

Post by KSquared »

lol... good point!

Thanks Burrito.
Post Reply