Reputation: 1900
Problem: I have several rows of results, for a single survey. Each survey can have any number of rows in the "Results" Table. There's a column called key_value. It's either 0 or -1. What is the fastest way in PHP to enumerate over several rows in a MySQL database, access each object, and flag a Boolean in PHP to tell me whether or not this particular survey has any rows with a key_value of 0?
something like this, except not brute forcing it...
for (i = 0; i < mysqlrows.length; i++){
if (mysqlrow.key_value == 0)
flag = true;
else
flag = false;
}
Upvotes: 1
Views: 53
Reputation: 4289
To operate on all matching (key_value = 0) Results:
$query = <<<EOD
SELECT result_id
FROM Results
WHERE key_value = 0
EOD;
$pdo = new PDO(....);
$stmt = $pdo->prepare($query);
if ($stmt->execute()) {
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result_id = $result['result_id'];
//do something with this result?
}
}
But if you only wanted the number of Results with key_value = 0, use some SQL like:
SELECT COUNT(*) as num_with_zero FROM Results WHERE key_value = 0
Upvotes: 1