mysql_query

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
alchemist
Forum Newbie
Posts: 7
Joined: Sun Mar 16, 2008 7:08 am

mysql_query

Post 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 :roll: )
While looking through phpmyadmin I have a member with username "john" in "members" table with certain value in "mail" field.
Any ideas? :banghead:
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: mysql_query

Post 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.
alchemist
Forum Newbie
Posts: 7
Joined: Sun Mar 16, 2008 7:08 am

Re: mysql_query

Post 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?
Thanaton
Forum Newbie
Posts: 2
Joined: Mon Sep 07, 2009 10:52 am

Re: mysql_query

Post 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
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: mysql_query

Post 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!
Post Reply