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!
$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?
'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.
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?
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.
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!