Build Your Own Database Driven Web Site Using PHP & MySQL, Fourth Edition

Share this article

This article was written in 2009 and remains one of our most popular posts. If you’re keen to learn more about PHP and MySQL, you may find this recent collection of articles on PHP of great interest.

PHP and MySQL have changed.

Back in 2001, when I wrote the first edition of this book, readers were astonished to discover that you could create a site full of web pages without having to write a separate HTML file for each page. PHP stood out from the crowd of programming languages, mainly because it was easy enough for almost anyone to learn and free to download and install. The MySQL database, likewise, provided a simple and free solution to a problem that, up until that point, had been solvable only by expert programmers with corporate budgets.

Back then, PHP and MySQL were special – heck, they were downright miraculous! But over the years, they have gained plenty of fast-moving competition. In an age when anyone with a free WordPress account can set up a full-featured blog in 30 seconds flat, it’s no longer enough for a programming language like PHP to be easy to learn; nor is it enough for a database like MySQL to be free.

Indeed, as you sit down to read this book, you probably have ambitions that extend beyond what you can throw together using the free point-and-click tools of the Web. You might even be thinking of building an exciting, new point-and-click tool of your own. WordPress, after all, is built using PHP and MySQL, so why limit your vision to anything less?

To keep up with the competition, and with the needs of more demanding projects, PHP and MySQL have had to evolve. PHP is now a far more intricate and powerful language than it was back in 2001, and MySQL is a vastly more complex and capable database. Learning PHP and MySQL today opens up a lot of doors that would have remained closed to the PHP and MySQL experts of 2001.

That’s the good news. The bad news is that, in the same way that a butter knife is easier to figure out than a Swiss Army knife (and less likely to cause self-injury!), all these dazzling new features and improvements have indisputably made PHP and MySQL more difficult for beginners to learn.

Worse yet, PHP has completely abandoned several of the beginner-friendly features that gave it a competitive advantage in 2001, because they turned out to be oversimplifications, or could lead inexperienced programmers into building web sites with gaping security holes. This is a problem if you’re the author of a beginner’s book about PHP and MySQL.

PHP and MySQL have changed, and those changes have made writing this book a lot more difficult. But they have also made this book a lot more important. The more twisty the path, the more valuable the map, right?

In this book, I’ll provide you with a hands-on look at what’s involved in building a database driven web site using PHP and MySQL. If your web host provides PHP and MySQL support, you’re in great shape. If not, I’ll show you how to install them on Windows, Mac OS X, and Linux computers, so don’t sweat it.

This book is your map to the twisty path that every beginner must navigate to learn PHP and MySQL today. Grab your favorite walking stick; let’s go hiking!

Go to page: 1 | 2

Frequently Asked Questions (FAQs) about PHP and MySQL

How can I connect to a MySQL database using PHP?

Connecting to a MySQL database using PHP involves using the mysqli_connect() function. This function requires four parameters: the hostname, the username, the password, and the database name. Here’s a simple example:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
In this code, we first define the server details and then use these details to establish a connection. If the connection is successful, it will print “Connected successfully”. Otherwise, it will print an error message.

How can I retrieve data from a MySQL database using PHP?

To retrieve data from a MySQL database, you can use the SELECT statement in SQL. This statement is used to select data from a database. The data returned is stored in a result table, called the result-set. Here’s an example:

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
In this code, we first define the SQL query and then use the query() function to execute the query. If the query returns any rows, it will print each row. Otherwise, it will print “0 results”.

How can I insert data into a MySQL database using PHP?

To insert data into a MySQL database, you can use the INSERT INTO statement in SQL. This statement is used to insert new rows into a database table. Here’s an example:

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
In this code, we first define the SQL query and then use the query() function to execute the query. If the query is successful, it will print “New record created successfully”. Otherwise, it will print an error message.

How can I update data in a MySQL database using PHP?

To update data in a MySQL database, you can use the UPDATE statement in SQL. This statement is used to modify the existing records in a table. Here’s an example:

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

$conn->close();
In this code, we first define the SQL query and then use the query() function to execute the query. If the query is successful, it will print “Record updated successfully”. Otherwise, it will print an error message.

How can I delete data from a MySQL database using PHP?

To delete data from a MySQL database, you can use the DELETE statement in SQL. This statement is used to delete existing records in a table. Here’s an example:

$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}

$conn->close();
In this code, we first define the SQL query and then use the query() function to execute the query. If the query is successful, it will print “Record deleted successfully”. Otherwise, it will print an error message.

Kevin YankKevin Yank
View Author

Kevin Yank is an accomplished web developer, speaker, trainer and author of Build Your Own Database Driven Website Using PHP & MySQL and Co-Author of Simply JavaScript and Everything You Know About CSS is Wrong! Kevin loves to share his wealth of knowledge and it didn't stop at books, he's also the course instructor to 3 online courses in web development. Currently Kevin is the Director of Front End Engineering at Culture Amp.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week