Turn Your Vehicle Into a Smart Earning Asset

While you’re not driving your car or bike, it can still be working for you. MOTOSHARE helps you earn passive income by connecting your vehicle with trusted renters in your city.

🚗 You set the rental price
🔐 Secure bookings with verified renters
📍 Track your vehicle with GPS integration
💰 Start earning within 48 hours

Join as a Partner Today

It’s simple, safe, and rewarding. Your vehicle. Your rules. Your earnings.

How to use PDO to read data from the database?

1) Write the regular select statement and again, instead of values, put named placeholders. For example:
[code language=”php”]
$sql = "SELECT * FROM users";
[/code]
2) Prepare the query:
[code language=”php”]
$query = $dbh -> prepare($sql);
[/code]
3) Execute the query:
[code language=”php”]
$query -> execute();
[/code]
4) Assign the data which you pulled from the database (in the preceding step) to a variable.
[code language=”php”]
$results = $query -> fetchAll(PDO::FETCH_OBJ);
[/code]
Here I used the parameter PDO::FETCH_OBJ that returns the fetched data as an object. If you’d like to fetch the data in the form of an array, use: PDO::FETCH_ASSOC.
5) Make sure that you were able to retrieve the data from the database, by counting the number of records.
[code language=”php”]
if($query -> rowCount() > 0){}
[/code]
6) In case that the query returned at least one record, we can echo the records within a foreach loop:
[code language=”php”]
if($query -> rowCount() > 0)
{
foreach($results as $result)
{
echo $result -> name . ", ";
echo $result -> city . ", ";
echo $result -> date_added;
}
}
[/code]
All code together now:
[code language=”php”]
$sql = "SELECT * FROM users WHERE city = :city";
$query = $dbh -> prepare($sql);
$query -> bindParam(‘:city’, $city, PDO::PARAM_STR);
$city = "New York";
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query -> rowCount() > 0)
{
foreach($results as $result)
{
echo $result -> name . ", ";
echo $result -> city . ", ";
echo $result -> date_added;
}
}
[/code]

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x