Forum Moderators: coopster

Message Too Old, No Replies

List Mysql column based on field

List Mysql column based on field

         

brian7742

5:06 pm on Dec 6, 2009 (gmt 0)

10+ Year Member



Hello,

I am currently putting together the User database which will keep track of what newsletters a user is subscribed to. My plan is to just use a separate column for each newsletter and then have a 1 or 0 for if the user is subscribed to said column.

What I can't figure out is how to simply list which newsletters (or columns) have a 1 as the field in php.

Any help would be greatly appreciated.

brian7742

5:19 pm on Dec 6, 2009 (gmt 0)

10+ Year Member



I've been trying to get the following to work:

mysql_query( "SELECT * FROM users WHERE * = '1'")

but something tells me this isnt the way to go about this.

brian7742

7:52 pm on Dec 6, 2009 (gmt 0)

10+ Year Member



Just in case anyone comes from a search, the replies on the following thread solved my issue: (different database design)

[webmasterworld.com...]

rocknbil

12:46 am on Dec 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you still might have a problem, I don't even know without testing what this would do. I've never used a star selector in a where (most call it a wild card, but not sure if that's it's official name in mySQL, manual says it's "shorthand for all fields [dev.mysql.com].")

mysql_query( "SELECT * FROM users WHERE * = '1'")

It should be select [field list, or *] from [table] where [fieldname] = 1;

If it's a numeric data type, quoting is not necessary. If you have the value in a variable,

select * from users where fieldname = $var;

and the variable is null or empty, this will give a mysql error - but in reality, this is a good thing. It reveals something in your programming is allowing a null/empty value to pass through when it shouldn't be.

I bring this up because,

select * from users where fieldname = '$var';

. . . will not error on a numeric field if $var is empty. It will just return no records, and you can spend hours trying to figure out why. :-)