0

Actually I have the following code:

$stmt = $pdo->prepare('SELECT id, city, date_created FROM my_table WHERE city = :city ORDER BY date_created DESC LIMIT 1');
$stmt->execute(array(':city' => $city));

I try to explain what I want to do:

  1. Check if there's a row containing all in column city.

  2. If so, check if the date of this row in date_created ist newer that the date in the newest entry for the query above. If so, select this row, else select the newest entry of the query below.

I hope you understand what I want to do. Unfortunately I'm not familiar with if/else statements in SQL.

Can anybody help me?

5
  • 1
    stackoverflow.com/questions/22252904/… and look under "Or a PDO method with a prepared statement:" - most likely a duplicate of this question. Commented Mar 15, 2017 at 16:37
  • 1
    Step 1: Add AND value = 'all' to your where clause Commented Mar 15, 2017 at 16:40
  • ... or an OR, depending on what you want to look for. Commented Mar 15, 2017 at 16:41
  • 1
    However, seeing point #2; the question's too broad. Commented Mar 15, 2017 at 16:45
  • 1
    The answer you posted I feel it should be part of your question Commented Mar 15, 2017 at 16:54

2 Answers 2

1

I think this could be simplified.

It looks like you should be able to just select rows where the city is either your parameter or 'all', order by date_created descending like you already are, and take the first row.

$sql = "SELECT id, city FROM news
        WHERE city IN(:city, 'all')
        ORDER BY date_created DESC LIMIT 1";

Since you know your query will only return one row, you can just use fetch instead of fetchAll to eliminate the unnecessary outer array.

$stmt = $pdo->prepare($sql);
$stmt->execute(array(':city' => $city));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
Sign up to request clarification or add additional context in comments.

Comments

0

Cause I'm not familiar with if/else statements in SQL but in PHP I made the following that's working:

<?php
$stmt = $pdo->prepare('SELECT id, city, date_created FROM news WHERE city = :city ORDER BY date_created DESC LIMIT 1');
$stmt->execute(array(':city' => $city));
$results = $stmt->fetchAll();
foreach( $results as $row ) {
$date_city = $row['date_created'];
}
$stmt = $pdo->prepare('SELECT id, city, date_created FROM my_table WHERE city = "all" ORDER BY date_created DESC LIMIT 1');
$stmt->execute();
$results = $stmt->fetchAll();
foreach( $results as $row ) {
$date_all = $row['date_created'];
}
if ($date_all > $date_city) {
$stmt = $pdo->prepare('SELECT id, city, date_created FROM my_table WHERE city = "all" ORDER BY date_created DESC LIMIT 1');
$stmt->execute(array(':city' => $city));
} else {
$stmt = $pdo->prepare('SELECT id, city, date_created FROM my_table WHERE city = :city ORDER BY date_created DESC LIMIT 1');
$stmt->execute(array(':city' => $city));
}
?>

2 Comments

this statment $stmt = $pdo->prepare('SELECT id, city FROM news WHERE city = "all" ORDER BY date_created DESC LIMIT 1'); $stmt->execute(); $results = $stmt->fetchAll(); can be reduced to $stmt = $pdo->query('SELECT id, city FROM news WHERE city = "all" ORDER BY date_created DESC LIMIT 1')->fetchall();foreach($stmt as $row)
It doesn't look like this should work, because $row['date_created'] shouldn't be set, since you're only selecting id and city in your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.