Tip:
Highlight text to annotate it
X
Here's where things start to get exciting. We have data in a database, and you built
up all of this knowledge about PHP and you know how to work with data in a lot of ways.
We haven't connected with a database yet, but you've worked with CSV files, you've worked
with arrays, and all of the concepts that apply to that type of data also apply to databases
but you get some extra benefits too. In fact, it can make some things a lot simpler than
before. But before we can use this data, we have to connect to the database in our PHP
script, so that's what our next step is.
Let's go to our resource pack directory and in the fourth folder, it's called MySQL_databases
and PHPMyAdmin, we're going to open up the first step here which is called test dot php
connect to mysql and query.
Also, if you don't have it open already, let's open the test dot php file from our examples
folder. So we'll hop over there and open up test dot php. Let's copy over the code from
our step file and paste it over the code that's in the test dot php file and save it. Now,
if you're using Acquia Dev Desktop, then these settings should work just fine for your installation.
However, if you went some other route, then the user name and password information for
this mysql_user might not be in line with what's in your installation.
Now, we'll get to more of that in a moment, but I just wanted to explain why this might
not work immediately on your machine. Since, we've copied over the code in test dot php,
let's demo what happens here. I'm going to go back to the browser, and I'm going to go
to test dot php and hit enter. And what I'm seeing here is each record in the database
displaying information about the rows and about each of the column names and the data
associated with each of the names. Here's George, here's Sally, and here's Deepak.
Now, let's look at the code that makes this happen. So I'm going back to my editor, and
the first thing we're doing is using a function called mysql_connect(). Now, for most of the
videos on BuildAModule, we use mysql and the majority of Drupal sites out there also run
on mysql. And so the reason that we have this mysql prefix on this function is because there's
other databases for which there are these same functions when we're trying to connect
to them with PHP. Mysql_connect() is a PHP native function. It comes bundled with PHP
just like some of these other mysql functions that we see here.
Now, just as a quick heads up, Drupal puts another layer on top of these functions so
you won't actually use them directly when you're working with Drupal, but it's important
to have an idea about what's going on behind the scenes, and occasionally there may be
an instance where you want to run a query directly.
Okay, so mysql_connect() is a function that we use to tell PHP that we want to start using
mysql. We can only use one database at a time so we specify the database and mysql also
has a set of users. And when we connect, we have to specify what user account we're going
to be accessing with.
Now, you didn't need to set up any users for mysql because Acquia Dev Desktop did that
for you. But oftentimes, if you're working with a live site, you want different users
that have different kinds of permissions. For example, you might want some users that
can read data but can't put anything into the database or delete anything from the database.
But for now we're going to keep this simple. When Acquia Dev Desktop installs, it installs
a user with a username drupaluser and a password that's empty. This runs on the server local
host, and that's the name of your webserver where your MySQL database is on. And then
our database name is my_database.
Now, you can see here with mysql_connect() that it takes three parameters. The first
is the server name, the second is the user name, and the third is the password. It doesn't
take the database name. We actually use a separate function called mysql_select_db()
in order to do that, and you see here we're using the database variable which is my_database.
The mysql_connect() function will return either true or false. If it's false, it means that
we didn't connect successfully and that can be because the server name is off, because
the user name is invalid, or because maybe our server hasn't been turned on yet. If it
returns false, then we probably want to know a little bit more about why it didn't work.
So we have a little bit of code here to display that error.
We check to see if connection is false by using this negation operator, and this should
be something that you're familiar with from previous videos. And if it's false, then we're
going to use this die() function. Die() will actually stop our PHP script from running
right here which we want to do here because everything we do from this point on depends
on our database. And we can pass a parameter to die() which is some text to display, basically
a shortcut or a print or a echo statement. And what we're saying is "You are not able
to connect to your database because" and then we use a function called mysql_error() which
returns the text of the error that happens. And you see we just wrap this in strong text
just to make it easier to read.
So if, for example, we put a couple of random characters in our server name here and we
save this and we jump back to the browser and refreshed, you'd see that we have an error.
It says �Warning: mysql_connect() Unknown MySQL server host 'localfdshost' in� and
then gives us a line number which will help us in troubleshooting, and it says "You were
not able to connect your database because Unknown MySQL server host 'localfdshost.'
So our error actually displayed a couple of times which is nice, but on certain server
configurations we're not going to get this original error and we have to display it explicitly
later.
All right, I'm going back to the editor, and I'm going to undo our garble. I'm going to
save it, and let's keep looking down the page.