What's the problem here?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

What's the problem here?

Post by Jim »

I'm getting an error in this little piece of code:

Code: Select all

$query = "CREATE TABLE $pre_members (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20), pass VARCHAR(20), email VARCHAR(100), rank VARCHAR(60))";
$create = mysql_query($query);
What's the problem? :P Gracias!
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

no idea. Just tried it myself and it worked fine.

What error message do you get if you add

Code: Select all

$create = mysql_query($query) or die(mysql_error());
Also, what value is in the variable $pre_members?

Or have you just accidentally put the dollar sign there?

Sam
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

It works :oops:

Code: Select all

$query = "CREATE TABLE ".$pre_members." (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20), pass VARCHAR(20), email VARCHAR(100), rank VARCHAR(60))"; 
$create = mysql_query($query);
Last edited by dethron on Thu May 02, 2002 7:18 am, edited 1 time in total.
Tiimmy
Forum Commoner
Posts: 38
Joined: Sat Apr 27, 2002 1:56 am
Location: Australia
Contact:

Post by Tiimmy »

Just a little note, when using variables within variables or functions, use them like this:

Code: Select all

$variable1 = "SOMETHING SOMETHING ".$variable2." SOMETHING ".function()." SOMETHING";
or

Code: Select all

function1 ("SOMETHING SOMETHING SOMETHING ".$variable." SOMETHING".function2());
You'll find that this will reduce the time it takes your script to execute, as this aids PHP in parsing your script.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Use single quotes instead of double quotes and PHP will be slightly faster. This is because stuff within single quotes doesn't get parsed by the PHP engine whereas stuff in double quotes does. The other benefit of using single quotes is that you don't have to escape the double quotes around attributes in HTML. Do use double quotes around SQL statements though because you'll need to use single quotes within them.

Examples:

Code: Select all

$variable1 = 'SOMETHING SOMETHING '.$variable2.' SOMETHING '.function().' SOMETHING';

function1 ('SOMETHING SOMETHING SOMETHING '.$variable.' SOMETHING'.function2());

echo '<img src="thispic.jpg" alt="This pic" />';

$sql = "SELECT id, info FROM table WHERE something LIKE '".$something."' ORDER BY id";
Mac

*** edit (clarification) ***
You won't get a massive speed increase if you use single quotes instead of double quotes. You most likely will only notice a subtle/smallish difference (if you time these things of course :)) if you have code that is called a lot or a loop with lots of iterations. The major benefit to using single quotes is being able to cut and paste HTML code into your PHP scripts and not having to go around escaping all the double quotes.
Last edited by twigletmac on Thu May 02, 2002 8:31 am, edited 2 times in total.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Actually, use single quotes so you don't have to escape the double quotes in your HTML. If your really looking for a speed increase, looking into the Zend Cache, faster hardware, or using C.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

FAST IS GOOD

Post by dethron »

Thanx all, I did not know the difference between them. Is there any link that can provide me with the ability of writing faster codes. :lol:
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

I fixed the error.

Now I just need to know how to use variables to decide the prefixes in my tables. Is it possible?

That's what the $pre is for :)
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

Is $pre_members set to anything?

what do you get if you

Code: Select all

echo $query;
When I try to execute your query without first setting $pre_members to anything I got the same error message as you, but with this added to the beginning:

Code: Select all

Warning: Undefined variable: pre_members in D:webdevsamscripts	est.php on line 4
You have an error in your SQL syntax near '(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20), pass VARCHAR(20),' at line 1
Do you have warnings for uninitialised variables turned on?

Try calling:

Code: Select all

error_reporting(E_ALL);
at the top of your script.

Hope this helps you out,
Sam
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

I've got the error fixed, Sam :)

Now I just need to know if you can use variables in the table creation code to create prefix names. :)

Also, after those tables are created, how do I forward a user to another page?

Thanks :)
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

Hi Jim, not sure how i misread your last post
I fixed the error
- think i'd better get some zzzzs :oops: :?

it should be fine to use the $pre_ - but its probably better to use it like:

Code: Select all

$query = "CREATE TABLE ".$pre."_members etc etc";
, to make sure php can tell where the var name ends.

Easiest way to forward someone to another page is with:

Code: Select all

header("Location: someotherpage.php");
exit();
As with cookies you need to make sure that no output has been sent before this or you'll get an error, and you need to terminate the script with exit after.

Sam :)
Post Reply