In our last article we discussed creating a MySQL database using phpMyAdmin, but what if we want to do it using PHP alone? We can do that. This tutorial will show how.
We will use four PHP functions:
In this example, we will create a database called “movies”. We will assume the MySQL server is on the localhost which it is if you are running WampServer 2 as recommended in our tutorials.
The following code will create our database. Please read the comments to understand what is happening.
<?php // First we will connect to our server. // Indicate the server address, username, and password. // If you have followed our tutorials since the beginning, your username is root. $link = mysql_connect("localhost","root","YOUR-PASSWORD-HERE"); // Now we will check to see if our $link variable shows we connected. if (!$link) // If it did not connect... { die('Sorry, I encountered this error: ' . mysql_error()); // The die function ends the script and uses the mysql_error to show us why we could not connect. } // If we've made it this far, then our connection was successful. // So now we will create our database using the SQL "create database" command. // Using the $link connection, send our request to the server to create our movies database. if (mysql_query("CREATE DATABASE movies",$link)) { // If successful, tell us it worked. echo "Database has been created."; } else // But if it doesn't work, tell us why... { echo "Could not create database: " . mysql_error(); } // Now that we are finished with the connection, we will close it. mysql_close($link); ?>
And there it is. If successful, you will now have an empty database called “movies”. In our next tutorial, we will learn how to create tables in the database.
Windows 7 Released
Windows 7 was officially released yesterday and was the buzz on many blogs. I didn’t realize that people had operating system launch parties but hey, some people need any excuse to drink! Besides, that Windows cake looks tasty.
I’ll be upgrading soon, just because I like to have the latest version. I stuck with XP for years (after sticking with Windows ME for years, wow!) and switched to Vista with my new PC a few months ago. People are hopeful about Windows 7 — hopefully it works out better than the Vista release did.
New Tutorial
I added one new tutorial this week: PHP Function Return Values.
Site Restructure
In Wordpress, you have Posts and Pages. Posts are used for blogging and Pages are used for static content, like an About or Contact page. I’ve been using the Pages to post my tutorials, but I thought about it and decided I’ll switch to using the Posts for them.
This will allow new tutorials to show up in the blog feed instead of requiring me to make a special post about the new pages on the site. I don’t know why I didn’t think about this to begin with, but it should be better for all you readers out there!
Just added two new tutorials about creating and using functions in PHP. I’m still working on another one or two tutorials about functions in specific. Functions are so powerful and important in PHP that I could probably write dozens of articles about them, but these first few tutorials will give beginners what they need to know.
Here’s the first two:
1. PHP Function
A function allows you to reuse a block of code throughout your application.
2. PHP More With Functions
Continue learning more about the power of PHP functions.
These are listed in our Building PHP Scripts, along with most of our tutorials.