...

View Full Version : How to select data from MySQL database with two 'Where' conditions?



sonic656
01-07-2012, 06:01 PM
mysql_query("SELECT * FROM users WHERE type='IZ' AND user_type='fo' AND reg_time='".$time."' ORDER BY `full_name`");

why this is not working?

melloorr
01-07-2012, 06:04 PM
mysql_query("SELECT * FROM users WHERE type='IZ' AND user_type='fo' AND reg_time='".$time."' ORDER BY full_name");


You do not need quotes around ORDER BY

tangoforce
01-07-2012, 06:26 PM
mysql_query("SELECT * FROM users WHERE type='IZ' AND user_type='fo' AND reg_time='".$time."' ORDER BY full_name");


You do not need quotes around ORDER BY

:rolleyes:

Supposing you have a column called order for a list of items you want displayed in a certain order. Try using "order by order asc" and you'll get an sql error.

BACK TICKS (not quotes) whilst not always necessary should not be discouraged. :thumbsup:

@sonic: Have you tried printing the query to be sure that the time value is correct? - Have you also tried running the query manually via phpmyadmin? - if there is a fault with the query then that will tell you. Alternatively you should use the mysql_error() function and print the error it returns.

melloorr
01-07-2012, 07:06 PM
I have just tried to put back ticks in my query, and it wouldn't order them

tangoforce
01-07-2012, 08:07 PM
It will if you're doing it right. There are some situations where it doesn't work such as in joins where you're naming tables along with columns.

melloorr
01-07-2012, 09:19 PM
It will if you're doing it right. There are some situations where it doesn't work such as in joins where you're naming tables along with columns.
I used

$query = mysql_query("SELECT * FROM users ORDER BY 'username' DESC")or die(mysql_error());

_Aerospace_Eng_
01-07-2012, 11:13 PM
Those are not backticks. They are just single quotes.

Backtick: `
Single quote: '

Button usually under the esc key is a backtick

tangoforce
01-07-2012, 11:18 PM
BACK TICKS (not quotes)


:rolleyes:

Again mellor, while you're trying to help you're giving out wrongful advice based on your inexperience with PHP. While its admirable that you are trying to help people you're still poking at shadows and you don't yet realise when you're out of your depth.

BACK TICKS are not quotes. You have quite clearly not taken the slightest bit of notice of my previous reply explaining this and you've not noticed the difference in the SQL in sonics opening post.

This is a quote: '
This is a back tick: `

If you can't see the difference then you need to do one of several things:

Change your screen resolution to something you can read
Use windows magnifier in the start>accessories folder
Put a magnifying glass against the screen


On my UK-Layout keyboard the backtick is the key to the left of number 1/! just above the tab key.

melloorr
01-07-2012, 11:37 PM
Thanks _Aerospace_Eng_

And sorry again tangoforce. While I admit I was wrong, I didn't actually say don't use back ticks, so 'wrongful advice' is a bit harsh. And you are right, my screen is 720p and my resolution is 1080p so it is quite difficult seeing the difference.
To be honest, I was thinking about getting an apprenticeship in PHP (funny, I know) but I doubt I will get hired with the stupid/silly mistakes I make.

Sorry, again, and you will probably be seeing less of me.

tangoforce
01-08-2012, 12:26 AM
While I admit I was wrong, I didn't actually say don't use back ticks, so 'wrongful advice' is a bit harsh

No, wrongful advice is correct. You advised the op to remove the quotes thinking that they were quotes and even giving a sample showing the back ticks being removed. That was wrongful advice and saying it wasn't over a technicality like the name of the marks when you've just supplied SQL showing exactly what you mean is not going to do you any favours.


Sorry, again, and you will probably be seeing less of me.

No need to try and put me on a guilt trip, I gave you advice last week about posting with little experience. This forum is a good forum for anyone interested in PHP and I'd hate to see you decide to leave just because someone has pointed out your errors because thats how you learn and become better at it. You clearly have something about you as a php programmer because I've seen some of your other replies showing people how to do some debugging. A lot of php newbies can't even work out how to do things like that so you have the potential to be a very good PHP programmer. Just don't try to take on too much and advise on things that you've not yet mastered thats all I'm trying to say.

As for the apprenticeship, if you can get such a thing then GO FOR IT. Thats what they're there for - to help you learn and earn a few quid.

melloorr
01-08-2012, 12:34 AM
I'm not trying to make you feel guilty, I'm just letting you know that I will not be posting as much because, as you say, I have not learned enough. So I will learn more.

tangoforce
01-08-2012, 12:41 AM
Fair enough but remember to come back here and if you can get that php apprenticeship then go for it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum