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.
Errors are an unfortunate fact of a programmers' life. So it's important to understand how MySQL Improved reports errors. In the MySQLI object oriented interface, errors are reported as the property of an object. Connection errors are properties of the database connection object. In other words, the instance of MySQLI class used to create the connection. Errors generated by the query method are also properties of the database connection object. However, errors generated by a prepared statement are properties of the statement object.
We'll be looking at prepared statements in depth in the next chapter. There are two properties that report connection errors. We met the first one, connect error, earlier in this chapter. It provides a text description of the error. If there's no error, the property is an empty string. The other property connect_errno, contains a numerical MySQL error code. This tends to be less helpful, although it might be useful if you want to display error messages in a language not supported by MySQL.
You can find the meaning of the error codes in the MySQL documentation. Errors generated by the query method and prepared statements both have the same properties. The only difference is that the query method stores the errors on the database connection object, whereas the statement object has its own error properties. The most useful property is simply called error. It contains a text description of the problem. If there's nothing wrong with your SQL error property is an empty string.
Errno contains the MySQL error code, while sqlstate contains the standard SQLSTATE error code. This is a five character code that can contain both numbers and letters. Finally, the error_list property was added in PHP 5.4. It's an associative array that gives you access to the other three properties rather than having to retrieve each one separately. However, in practice, the only property you're likely to need is error which contains a text description of the problem.
This is mysqli_error.php which you can find in the chapter five 05_07 folder of the exercise files. The name of the database tape will end the select query on line four is misspelled. So if we load this page into a browser, we'll get an ugly error message. It's not only ugly, it's not very informative. Fatal error: Call to a member function fetch_assoc on a non-object.
Well, it's technically correct, but MySQLi will give us a much better indication of what's going on. So, let's go back and after submitting the query we'll insert a new line. Your first instinct might be to miss the arrow property of the result object, but the result object doesn't have an arrow property. This code uses the query method to submit the query, so the arrow property is on the database connection. So, if then db for the database connection.
Get the error property. And if it has a value, we'll assign it to error. The important thing to note is that the error property is always set. So don't use is set to check for it. If it has a value, db->error will equate to true. But if there's no problem, it will be an empty string, which equates to false. So that's why we've been able to use db->error directly as the condition. If there is a problem, we need to prevent the code from attempting to access the nonexistent results object, and that is being used down here on line 32.
So what we need to do is we're going to display the error up here in this conditional block on lines 22 to 24. So if we wrap the display in an else block, if there is an error, nothing will be displayed. So we need to put the closing curly brace after everything has been displayed so that goes after the table. There's a PHP block down here that is closing the database connection, so we can put the closing curly brace in there.
So if we now save that, I'll go back to the browser and refresh the page. This time we get the error message from my MySQLi. Table oophp nams doesn't exist, so that's where the problem is. Let's go back, fix that. Correct the name. Save and reload the page. This time no error message. We get the database result displayed as expected. The main point to remember about using the query method is that error messages are stored as a property of the database connection object and not the result object. The result object does not have an error property. The error property is always set, but it contains a value only if there's a problem with the SQL query. On final point, display MySQL error messages only when developing and debugging. Replace them with more neutral messages when deploying your webpages to a live website.
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.