Viewers: in countries Watching now:
Now that PHP has true object-oriented capabilities, it's best practice to access databases using PDO (PHP Data Objects) and MySQLi. These methods produce database-neutral code that works with over a dozen systems, including MySQL, SQL Server, PostgreSQL, and SQLite. Learn how to use PDO and MySQLi to perform basic select, insert, update, and delete operations; improve security with prepared statements; and use transactions to execute multiple queries simultaneously. Author David Powers also covers advanced topics like instantiating custom objects, and compares PDO to MySQLi so you can decide which method is right for you.
In my SQL improve, the query and run query methods handle only single queries. If you want to submit multiple queries in a single operation you need to use the multiquery method. This is mysqli_multi.php which you can find in the chapter seven 07_04 folder of the exercise files. We use this to execute three select queries in succession and then loop through the results. To avoid a lot of repetitive typing, I'm going to paste in the code from query.txt, which you can find in the exercise files for this video.
The new code needs to go after the database connection. So create a new line on line four and paste in the code. Line four defines an array with the names of three car manufacturers. And the rest of the code is a select query to get the minimum and maximum price for cars for the first manufacturer in the makes array. To submit several queries in succession each query needs to be separated by a semicolon, so we need to put that semicolon just before this closing double quote here. So, insert your cursor on line seven between the single quote and double quote and type a semicolon. Then we need to select everything between the double quotes. So that's that semicolon there, and right up to the word select, but not the double quotes. And duplicate that twice. And just make things a little bit tidier, put the select beginning on a new line. And then we need to change the value of makes zero in the second and third one. So this becomes makes one. And this becomes makes two. So what we're doing is we're submitting the same query three times by getting the values from the array up here, Chrysler, Ford, and then Toyota. So we've got three select queries and each one is separated by a semi colon. We can now submit that complete block of code using the multi query method and we'll do that down in the body of the page, here on line 30 inside the else block.
And we simply call the database object, and it's multi-query method, and we pass it the SQL. And this will submit all three queries as a single block, but the results won't be buffered. To get each result set, we need to create a do while loop. So let's put that on the next line. Do, then a block. And after the closing curly brace, we need the while condition. And the while condition needs to check if there are any more results using the next result method of the database object.
So while DB, and then next result. And inside the do while loop, we deal with each result as it's retrieved. To get each result as we go through the loop, we need to call the store result method. So we'll create a variable to store the result. And then call the database object and the store result method. This returns a mysqli result object, so you can treat it in exactly the same way as if you were using query. In this example, each select query is going to return only one row, so we won't need an inner loop inside the dual loop, but we'll just use the fetch us up method to get each row and then display the result.
So, row equals then the result object, fetch assoc and we can then display the results. So, we'll have a double quoted string and we'll have an H2. Because we are using an array element we need to put that inside curly braces otherwise we get a nasty error. So row and then make that closing curly brace. And then, the closing H2 tag and on the next line we’ll create a paragraph.
And we'll format the price using the number format function, so we need to pass that row, min price. And we concatenate more text on the end, to a dollar sign. And format the next one, number_format($row'maxprice'). Just need our closing paragraph tag.
When we get to the end of the loop ,we no longer require the result. So we'll free it using the free method. So result free. And this will now go through all of the results in turn and display them. So let's just save that and test it in the browser. And there are the results from the queries that were submitted in one single operation. So let's just go back and review the code. If we go back up to the top, we have a single block of SQL with three queries in there, each of them separated by a semicolon.
Then we submit it using the multi_query method of the database object. And then create a do while loop. The while condition checks to see if there are any more results using the next_result method of the database object. Inside the do while loop, we store the result using the store_result method, and then you handle it in exactly same way as any other result set. But at the end of the loop you free the memory associated with the results using the free method.
In this example the multi-query method has been used only with select statements but you can use other SQL queries such as insert, update and delete or even a mixture of different types. Just separate each query with a semi-colon. and if only some queries are select statements, use a conditional statement inside the do all loop to check whether store result produces a result set. One other important point, the multi query method does not support prepared statements.
There are currently no FAQs about Accessing Databases with Object-Oriented PHP.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.