Output the last row inserted in MySQLi

DISCLAIMER: This post may contain “affiliate” links to products and services I recommend. I’ll receive a small commission if you decide to purchase one of these products or services. I only recommend products I genuinely believe will help you in running your freelance business.

MySQLi makes grabbing the last inserted row easy. After running your insert query, you can do this:

$id = $mysqli->insert_id;

Then, you can query for that row like this:

$result = $mysqli->query("SELECT * FROM table_name WHERE ID = {$id}");

And, fetch the result as an object:

$user = $result->fetch_object();

From there it’s a simple echo to output it to your page:

echo $user->user_name;

(Although, you’ll want to use htmlspecialchars or htmlentities to escape the output and prevent XSS attacks. But, we’ll save that for another day.)

Simple.

Anyway, in the newly released Module 3 of PHP 101, I show you how to build a fully-functioning PHP form that submits and retrieves data from your database… so you’ll learn how to do this with all the bells and whistles like prepared statements, input filtering, output escaping and more.

It’s a simple proposition…

If you want to master all the ins and outs of MySQL…

This is how you do it.

Right now, you can grab all three modules for just 27 buqs.

That’s 10 buqs off the regular.

Here’s the link

Later,

John

You might also like

Do you want more freelance clients?

I’ll show you what I learned over the last 15 years to grind out (from absolute scratch) a backlog of new clients wanting to hire you. Who your best client prospect are, what services you should be offering them, where to find them and more. Just enter your email address in the box below and let’s get started:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

Do you want more freelance clients?

Enter your email below to get started building your system for consistently bringing in new freelance clients:

WHAT OTHERS ARE SAYING

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Sukh Plaha

John is a fantastic and patient tutor, who is not just able to share knowledge and communicate it very effectively – but able to support one in applying it. However, I believe that John has a very rare ability to go further than just imparting knowledge and showing one how to apply it. He is able to innately provoke one’s curiosity when explaining and demonstrating concepts, to the extent that one can explore and unravel their own learning journey. Thanks very much John!

Ray Edwards

I recommend John every chance I get. If every person I worked with were as committed to excellence, punctuality, value, and unquestionable integrity… the world would be a better place. Highest recommendation.

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.

Close Menu