Output the last row inserted in MySQLi

00014

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

Leave a Reply on Twitter

Due to excessive spam, I’ve disabled comments on my website. If you have a question or would like to leave a comment, don’t hesitate to contact me on Twitter.

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

Chris Aitken

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

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Bradley Smith

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

Thabo Motsoahae

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

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

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.

Lori Grant

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