I have a table 'jobs' with fields id,p1,p2,p3 where some entries under p1,p2,p3 have a value of '1'. How might I get a simple list of fields (p1,p2, or 3) which have a value of '1' in rows of table jobs where id=$id.
Asked
Active
Viewed 81 times
0
-
Let me clarify. I don't want to actually list the fields in the query because there are a lot of them and they change from time to time. I am at a loss as to where to start. – Earnest Overly Nov 08 '12 at 18:55
-
that sounds like a poor db design, that's probably where you need to start. – Nov 08 '12 at 19:59
4 Answers
0
SELECT * FROM jobs WHERE id=$id AND (p1 = 1 OR p2 = 1 OR p3 = 1)

Greeso
- 7,544
- 9
- 51
- 77
-
I don't want to actually list the fields in the query because there are a lot of them and they change from time to time. – Earnest Overly Nov 08 '12 at 18:56
-
0
Try:
SELECT p1, p2, p3 FROM jobs WHERE id=$id AND (p1 = 1 OR p2 = 1 OR p3 = 1)

Teena Thomas
- 5,139
- 1
- 13
- 17
0
$row = mysqli_fetch_assoc($query_result);
$list = '';//or array
foreach ($row as $key=>$val) {
if ($key != 'id' and $val == 1) { //or if substr($key, 0, 1) == 'p' and $val == 1)
$list .= $key.', ';
}
}

Alvar FinSoft Soome
- 740
- 5
- 11
-
Rather than only post a block of code, please *explain* why this code solves the problem posed. Without an explanation, this is not an answer. – Martijn Pieters Nov 08 '12 at 18:52
-
This worked with minor changes. I did it like this:foreach ($key as $key => $val) { if ($key != 'id' and $val == 1) { echo $key; // Field name}} } } – Earnest Overly Nov 08 '12 at 21:00