Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hotwire132002

macrumors 65816
Original poster
Jan 24, 2004
1,209
1
Cadillac, MI
Hey,

I'm working on a newsroom automation script in PHP. I've got just about everything down, except for one thing. I'm trying to create a page that will create a new table--you know, you enter the name for the table in a textbox, then the script runs a MySQL query to create the table.

This is probably a stupid question, but I can't get this to work. I want to plug in a script that basically runs the following query:

CREATE TABLE '$table' (id int(6) NOT NULL ... etc etc

I think the problem with the above line of code is the fact that MySQL recognizes the apostrophes to be the end of the query--but if I try to run the query without them, it names the table $table. Does anyone have a solution to this?

As you can probably tell, I'm still learning PHP.
 

hotwire132002

macrumors 65816
Original poster
Jan 24, 2004
1,209
1
Cadillac, MI
Nermal said:
I'm still new to PHP, but try:

CREATE TABLE ' . $table . ' (id int....

No luck. Any other ideas?

EDIT: I made it work! Slight glitch in your code, Nermal--using " instead of ' worked for some reason. The . $table . code did the trick, though! Thanks!
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Yeah, for some functions there is a difference between single and double quotes. Always check the documentation when in doubt. :)
 

MontyZ

macrumors 6502a
Jan 7, 2005
887
0
The reason the single quotes didn't work is because PHP will only recognize a variable within a text string that is delimited with double-quotes. If you put the variable in single quotes, PHP intreprets it literally.

$var = 'Apple';

echo 'Eat this $var' // will be interpreted as Eat this $var

echo "Eat this $var" // will be interpreted as Eat this Apple
 

pulsewidth947

macrumors 65816
Jan 25, 2005
1,106
2
The other thing worth mentioning is PHPMyAdmin. Thats a web gui for MySQL, but the advantage is you can insert things into tables and tell it to generate the PHP code for what you just did, which you can then look at and edit for your PHP pages.

Maybe not the best explanation, but it really is useful.
 

ThatSoundAgain

macrumors member
Apr 10, 2005
52
0
pulsewidth947 said:
The other thing worth mentioning is PHPMyAdmin. Thats a web gui for MySQL, but the advantage is you can insert things into tables and tell it to generate the PHP code for what you just did, which you can then look at and edit for your PHP pages.

Maybe not the best explanation, but it really is useful.

I second that. I'm lucky that my webhost has it installed and running by default. It's invaluable for debugging homebrew-backends. It lets you inspect data to make sure everything goes where it's supposed to, and you can even type in queries and get feedback on any errors.

That said, I think that there's even a third kind of quotes that MySQL uses, the ` or accent grave (normally is used as an accent, like this: è à). My queries typically look something like this:

$query = "INSERT INTO `blog` (`title`,`text`,`date`) VALUES ('$title','$text','$date');";
$result = mysql_query($query);

Names of tables and fields go in accent grave-quotes, values go in single. The whole thing goes in doubles.

I got into the habit of using this format since I couldn't get it to accept table-names with hyphens in them otherwise. Or was it underscores? I forget.
 

hotwire132002

macrumors 65816
Original poster
Jan 24, 2004
1,209
1
Cadillac, MI
pulsewidth947 said:
The other thing worth mentioning is PHPMyAdmin. Thats a web gui for MySQL, but the advantage is you can insert things into tables and tell it to generate the PHP code for what you just did, which you can then look at and edit for your PHP pages.

Maybe not the best explanation, but it really is useful.

I usually use PHPMyAdmin--but for the script I was working on, I needed to create tables without logging in to my web site admin panel. So, I needed the variable solution. PHPMyAdmin is a lifesaver, though! I doubt I'd be doing as much with MySQL as I am without it!
 

yklxcq

macrumors member
Jun 22, 2005
81
0
pulsewidth947 said:
The other thing worth mentioning is PHPMyAdmin. Thats a web gui for MySQL, but the advantage is you can insert things into tables and tell it to generate the PHP code for what you just did, which you can then look at and edit for your PHP pages.

Maybe not the best explanation, but it really is useful.
Does the phpMyAdmin support mac platform?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.