Page 1 of 1
mysql_query
Posted: Mon Sep 07, 2009 10:41 am
by alchemist
here is a fragment from the code:
Code: Select all
$query="SELECT mail FROM 'members' WHERE username='john'";
$result=mysql_query($query)
or die("could not execute query");
$mail=mysql_fetch_array($result,MYSQL_ASSOC);
and I get "could not execute query" output.
It seems to me that there is no mistake in this three lines. (if there is, please forgive me, I am a comlete beginner

)
While looking through phpmyadmin I have a member with username "john" in "members" table with certain value in "mail" field.
Any ideas?

Re: mysql_query
Posted: Mon Sep 07, 2009 10:49 am
by turbolemon
'members' should be `members` or just members (without quotes). You only need to quote table names if they contain reserved words, btw. Different quote types have different meanings in SQL.
Re: mysql_query
Posted: Mon Sep 07, 2009 10:59 am
by alchemist
turbolemon wrote:'members' should be `members` or just members (without quotes). You only need to quote table names if they contain reserved words, btw. Different quote types have different meanings in SQL.
Hint was really great. At least, it helped me to solve the problem.

Many Thanks!
P.S. you mean, I can use reserved words in table names provided that I'll use quotes?
Re: mysql_query
Posted: Mon Sep 07, 2009 11:00 am
by Thanaton
And, no offence, there are some other things 'wrong' in your script:
1. NEVER use 'die' or 'exit', except when you really want to stop executing the whole script (for example after a header('Location: ...')). When not using 'die' or 'exit' you can use 'if ... else' statements to execute some errorcode.
2. mysql_fetch_array($result, MYSQL_ASSOC); is the same as using mysql_fetch_assoc($result);
3. In your errorcode, and when debugging, you can use 'mysql_error()' to show you the last error, if any.
Hope I helped a bit!
Greeting,
Thanaton
Re: mysql_query
Posted: Mon Sep 07, 2009 11:17 am
by turbolemon
Yes you can:
Code: Select all
CREATE TABLE `SELECT` (
id int(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT
)
Tested on MySQL 5.0.51
You would have to try it for yourself on your server

. Valid for field names too. I would tend to avoid it though, as a best-practice; it leads to hair-pulling if you are working in a team (or just forget further down the line). Let's just say mileage may vary!