Hello to all, welcome to therichpost.com. In this post, I am showing PDO select query with case statement. First time I am showing mysql pdo case statements.
Here is the working code for PDO select query with case statement:
<?php
$servername = “localhost”;
$username = “root”;
$password = “root”;try {
$conn = new PDO(“mysql:host=$servername;dbname=user”, $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo “Connected successfully”;
}
catch(PDOException $e)
{
echo “Connection failed: ” . $e->getMessage();
}
$upcoming_appointment = $conn->prepare(“SELECT user.*,
CASE
WHEN user.field = ‘jquery’ THEN ‘love’
ELSE ‘okay’
END AS ‘field_status’
FROM `user`”);
$upcoming_appointment->execute();
$upcomingappointments= $upcoming_appointment->fetchAll(\PDO::FETCH_OBJ);
echo ‘<div class=”container”><table class=”table table-dark”>
<thead>
<tr>
<th scope=”col”>#</th>
<th scope=”col”>Name</th>
<th scope=”col”>Field</th>
<th scope=”col”>Intrest</th>
</tr>
</thead><tbody>’;
foreach ($upcomingappointments as $BOOKING) {
echo “<tr>”;
echo “<td>”.$BOOKING->id.”</td>”;
echo “<td>”.$BOOKING->name.”</td>”;
echo “<td>”.$BOOKING->field.”</td>”;
echo “<td>”.$BOOKING->field_status.”</td>”;
echo “</tr>”;
}
echo ‘</tbody>
</table></div>’;
?>
There are so many queries in pdo for mysql data and I will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com
Recent Comments