Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
Prepared statements are a feature of MySQL, and of many other databases. The topic is too advanced for an introductory tutorial, but I do think it's important for you to understand what they are. The general idea is that you give MySQL a template for a query that you want to run, with places indicated where you can fill in the blanks later. For example, we might have an insert statement, and in place of the three bits of data that we would be dropping into the columns, we would put question marks. Those are place holders for where the data is going to go later. Then when we want to run the query: we call up the template, fill in the blanks and then MySQL to run it. So why do it this way, why go that extra step? Well, because when any query runs, the database has to parse the query and develop a plan for running it. With prepared statements, the database will do this work once and then you can reuse it for future queries where only the variables change. The parsing and the plan of attack stay the same. This can be faster, especially if you're doing complex queries, repeating a query many times.
But more importantly, prepared statements separate the query from the dynamic data. This completely, 100% eliminates the possibility of SQL injection. It even eliminates the need to escape your values to prevent it. Because the blanks where the data goes are so clearly defined, and the data that's going in there, is going to be cast into the right type. So the database will handle it for you, it'll make sure that it puts the right kind of data in the right spot, and it won't accidentally inject SQL. Now, as I said, we aren't going to be using these in this tutorial, but they have come into pretty common usage in PHP, so I think you should at least recognize them. Let's take a quick peek at an example before we move on. So, this would be an example of a prepared statement. You would open up your database connection, you would design your query, but then you would have question marks in place of username and password, where the data would be dropped in.
Those are the fill in the blank spots. Then you would call mysqli_prepare to prepare the statement, and then from on we have a statement object we can work with. The magic of this happens with the bind_param function. so, mysqli_stmt_bind_param takes variable values and drops them into those spots and that's where the escaping takes place. After that we tell it to execute the statement. We get back the results, we have to do a little bit of extra work with those, but in the end we can work with that data the same way that we normally do. Now, there are a few quirks with prepared statements that make them a little bit harder to pick up, until you've had more experience with PHP.
So for now, you should stick with what we've learned and query the database the standard way and escape your values using mysql real escape string. At least now you know what lies ahead for you when you do start working with prepared statements. (BLANK_AUDIO)
Find answers to the most frequently asked questions about PHP with MySQL Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
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.