Book Image

Practical Web Development

By : Paul Wellens
Book Image

Practical Web Development

By: Paul Wellens

Overview of this book

Table of Contents (23 chapters)
Practical Web Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
10
XML and JSON
Index

MySQLi in PHP


Although we omitted this on purpose in Chapter 5, PHP, PHP has features found in object-oriented programming languages. So, rather than write functions, you can instead create objects and write methods for them. This is exactly how the PHP programming interface to MySQL works: there is one dedicated object, mysqli, and a number of methods. With these methods, you can connect to a database, submit queries, fetch the result, and finally close the connection.

Consider that we have a web application that is an online bookstore. The name of our database is bookstore, for which we created a user bookuser, with book**4u as the password. The database itself contains at a minimum the table's books and authors.

Let's write our first PHP program using MySQL now.

Connecting to the database

First we need to connect to the database. To do so, we need to identify which database we want to connect to, on which host the database server is running, and which database user we want to use. The host...