Jump to content

Archived

This topic is now archived and is closed to further replies.

esahp

PHP/MySQL Issue

Recommended Posts

I have:
[code]
$query = "SELECT * FROM users WHERE 1 AND WHERE `status` = '1'";
[/code]
and get the error:
[quote]
Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `status` = '1'' at line 1
[/quote]

How do I get this working correctly?

Share this post


Link to post
Share on other sites
$query = "SELECT * FROM users WHERE 1 AND WHERE `status` = '1'";

Do you mean to say this?

$query = "SELECT * FROM users WHERE `status` = '1'";

Share this post


Link to post
Share on other sites
Why do you have the 'WHERE 1 AND' there? You need to specify what is equal to '1' which you have done with 'status'. Is there another column you want to pull that equals '1'?

Share this post


Link to post
Share on other sites
Well, I'm completely new to the SQL language, and I was told [b]WHERE 1[/b] lists all of the data in the table and I was looking around and found [b]WHERE `blah` = 'blah'[/b] Which I assumed to mean [b]WHERE [u]field[/u] equals [u]blah[/u][/b].
So basically, I'm wanting to to list all of the data in the table [b]users[/b] where the field [b]status[/b] is equal to [b]1[/b]

Share this post


Link to post
Share on other sites
Try this:


$query = "SELECT * FROM users WHERE `status` = '1'";


You only need to say WHERE once. So if you had more than one argument you would say 'status'='1' and name='Matt'

Share this post


Link to post
Share on other sites
That is what the '*' does. If you only wanted to select 'id' and 'name', your query would look like this:

[CODE]SELECT id, name FROM table WHERE status = 1[/CODE]

So if you want to pull all columns, use *. The 'WHERE' clause lets you specify certain information about your results.

Share this post


Link to post
Share on other sites
I see, thanks guys.
That line without [b]WHERE 1 AND[/b] also worked, thankyou.

Share this post


Link to post
Share on other sites
esahp,

There's nothing wrong with [b]SELECT * FROM table WHERE 1[/b] What you've read elsewhere was correct.

In fact, your original query only had one thing syntactically wrong with it, it had the additional WHERE in there.  When providing a second clause, just use the AND or OR.

[b]SELECT * FROM table WHERE 1 AND status = 1[/b]

Regards
Huggie

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.