Categories

Friday, April 19, 2024
#919814419350 therichposts@gmail.com
MysqlPDOPhp

PDO select query with case statement

PDO select query with case statement

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.

pdo-mysql-data

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.