Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Executing simple non-SELECT queries

From: Accessing Databases with Object-Oriented PHP

Video: Executing simple non-SELECT queries

We have seen how to use the query method to submit select queries. So, our database connector, db.

Executing simple non-SELECT queries

We have seen how to use the query method to submit select queries. You can also use it for non-select queries but there is another method called exact that's probably more useful for insert, update and delete queries. Let's take a look at both query and exact and see how they differ. This is query_insert.php, which you can find in the chapter 2, 02_07 folder of the exercise files. On lines four and five is an insert statement that adds a new entry to the names table.

So, let's submit that using query. And we'll restore the return value as result. So, our database connector, db. Then the query method, and we pass it the SQL. And to find out what result contains we'll use var_dump and pass it result. So, if we save that and then load the page into a browser. The result that we get is objects PDOStatement public queryString and then the value of the insert statement that we've just used.

This isn't really very useful because this works only when query succeeds. If the query fails, it returns false. But if you do want to inspect the contents of a non-select statement that is submitted using query, then you can use the queryString property. Let's just see how that's done. So, instead of dump, we can use echo and then it's result and the arrow operator queryString. And if we save that, go back to the browser and refresh the page.

It'll insert the same value again but that doesn't really matter, this is a test database. So if we refresh it, there's is that insert statement, that succeeded using the query method. But when you're using a non-select statement, a statement that is going to make changes to your database, it's usually more useful to get details of what's actually happened. And that's where the exact method comes in. So let's go back to the editing program and change to exec_insert.php.

That's in the same exercise folder as 02_07. We've got exactly the same insert statement here. This time, instead of using query, we're using the exec method. So that's the only difference. And we're using var_dump to see what that contains. So let's just load that into a browser. And what we've got here is int 1. Basically, what it's doing is it's telling us that one row has been affected in our database.

That's much more useful. So, let's go back to the editing program and we'll change result to affected, and instead of dump what we'll do is we will echo affected row inserted. And it's also useful at times to find out if it's being given and id, this only works with some databases, and it relies on the database using auto increment ids, the way in which you can get that is using the last insert id method.

So, if we do that With ID and then concatenate on the end of that. It's the database object that contains the last insert ID method. So let's just get that lastInsertId. And then if we save that and go back to the browser. Refresh, we've now got one row inserted with ID 14. Although this can be rather useful, you need to be careful with the lastInsertId method.

It doesn't work in all databases and if the method isn't supported, it generates an error. And we'll be looking at error handling in the next video. And if you're using PostgreSQL, you need to pass the name of a sequence object as an argument to lastInsertId. And EXEC works exactly the same way with UPDATE and DELETE. We've got a lot of Williams in our database now so let's delete them. Go back to the editing program and exec_delete.php.

Delete from names where name equals William, so we'll create a variable called affected and then our database object and the exec method. We parse it at sql. And we can then echo affected records deleted. So we just save that. And I see I've got a little red error message up here. Yes, what we need is the concatenation operator in there.

Everything's turned green. Save that and then load it in the browser. Four records have been deleted. Wonderful. With two methods to choose from, the question arises as to which to use. The PDO query method returns different values depending on the type of query. With the select query, the returned value is the result set, but with insert update and delete queries, you simply get back a string containing the query that's just been executed. The PDO exec method always returns the number of rows affected.

So, exec is the better choice for INSERT, UPDATE and DELETE, but exec should should never be used for SELECT queries because it doesn't return a result set.

Show transcript

This video is part of

Image for Accessing Databases with Object-Oriented PHP
Accessing Databases with Object-Oriented PHP

47 video lessons · 2030 viewers

David Powers

Expand all | Collapse all
  1. 13m 33s
    1. Welcome
      1m 4s
    2. What you should know before watching this course
      2m 8s
    3. Using the exercise files
      4m 56s
    4. Setting SQLite permissions
      1m 11s
    5. A quick primer on using PHP objects
      4m 14s
  2. 10m 12s
    1. Overview of PHP database APIs
      4m 5s
    2. Using prepared statements
      4m 24s
    3. Using transactions
      1m 43s
  3. 48m 57s
    1. Creating a database source name
      2m 3s
    2. Connecting to a database with PDO
      7m 27s
    3. Looping directly over a SELECT query
      3m 49s
    4. Fetching a result set
      8m 3s
    5. Finding the number of results from a SELECT query
      7m 14s
    6. Checking if a SELECT query contains results
      3m 32s
    7. Executing simple non-SELECT queries
      6m 2s
    8. Getting error messages
      7m 17s
    9. Using the quote() method to sanitize user input
      3m 30s
  4. 39m 51s
    1. Binding input and output values
      2m 36s
    2. Using named parameters
      9m 51s
    3. Using question marks as anonymous placeholders
      2m 35s
    4. Passing an array of values to the execute() method
      5m 20s
    5. Binding results to variables
      7m 53s
    6. Executing a transaction
      6m 54s
    7. Closing the cursor before running another query
      4m 42s
  5. 21m 20s
    1. Generating an array from a pair of columns
      2m 44s
    2. Setting an existing object's properties with a database result
      4m 42s
    3. Creating an instance of a specific class with a database result
      6m 1s
    4. Reusing a result set
      7m 53s
  6. 38m 14s
    1. Connecting to a database with MySQLi
      5m 57s
    2. Setting the character set
      1m 57s
    3. Submitting a SELECT query and getting the number of results
      4m 4s
    4. Fetching the result
      7m 35s
    5. Rewinding the result for reuse
      3m 20s
    6. Handling non-SELECT queries
      5m 27s
    7. Getting error messages
      5m 47s
    8. Sanitizing user input with real_escape_string()
      4m 7s
  7. 27m 49s
    1. Initializing and preparing a statement
      4m 17s
    2. Binding parameters and executing a prepared statement
      5m 55s
    3. Binding output variables
      5m 6s
    4. Executing a MySQLi transaction
      7m 5s
    5. Dealing with "commands out of sync" in prepared statements
      5m 26s
  8. 24m 7s
    1. Buffered and unbuffered queries
      4m 19s
    2. Using real_query()
      6m 1s
    3. Freeing resources that are no longer needed
      2m 31s
    4. Submitting multiple queries
      6m 41s
    5. Creating an instance of a class from a result set
      4m 35s
  9. 3m 31s
    1. PDO and MySQLi compared
      3m 31s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed Accessing Databases with Object-Oriented PHP.

Return to your organization's learning portal to continue training, or close this page.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?


Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.