Jump to content


Photo

PHP/MySQL Issue


  • Please log in to reply
7 replies to this topic

#1 esahp

esahp
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationUSA

Posted 26 September 2006 - 02:21 AM

I have:
$query = "SELECT * FROM users WHERE 1 AND WHERE `status` = '1'";
and get the error:

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


How do I get this working correctly?

To obtain peace and quiet, get a Phoneless Cord.


#2 livepjam

livepjam
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 26 September 2006 - 02:37 AM

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

Do you mean to say this?

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

#3 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 26 September 2006 - 02:40 AM

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'?

#4 esahp

esahp
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationUSA

Posted 26 September 2006 - 02:42 AM

Well, I'm completely new to the SQL language, and I was told WHERE 1 lists all of the data in the table and I was looking around and found WHERE `blah` = 'blah' Which I assumed to mean WHERE field equals blah.
So basically, I'm wanting to to list all of the data in the table users where the field status is equal to 1

To obtain peace and quiet, get a Phoneless Cord.


#5 livepjam

livepjam
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 26 September 2006 - 02:45 AM

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'

#6 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 26 September 2006 - 02:47 AM

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

SELECT id, name FROM table WHERE status = 1

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

#7 esahp

esahp
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationUSA

Posted 26 September 2006 - 02:49 AM

I see, thanks guys.
That line without WHERE 1 AND also worked, thankyou.

To obtain peace and quiet, get a Phoneless Cord.


#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 September 2006 - 08:57 AM

esahp,

There's nothing wrong with SELECT * FROM table WHERE 1 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.

SELECT * FROM table WHERE 1 AND status = 1

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users