We need to make our life easier by creating consistency for every machine. What this means is every machine you try to use, whether it be yours or your colleagues, the environment will be the same when you sit down to help them with something. This is very important if you’re a Development Manager and you’re trying to help one of your staff.
This is one of several posts in regards to this subject.
This post will show how to do a verification that PHP is talking to our MySQL database for the Apple Macintosh. We’ll go back and show how to do it for Windows in a later post.
So, to do a test, to make sure we’re talking to the database, we start up NetBeans and click on “File”, “New Project” which will give us this dialog box:
Clik here to view.

New Project PHP Application
Now click on “PHP”, “PHP Application” and click “Next” which will give us a dialog box that looks like this:
Clik here to view.

New PHP Project
We’re entering “PhpDbTest” for the “Project Name:” and need to make sure that our “Sources Folder:” is “PhpDbTest” as well. Click “Next >” and you should see this dialog box:
Clik here to view.

New PHP Project (Project URL)
As noted when we set up PHP, I run my applications out of my “/Users/thcampbell/Sites” directory so I click the “Copy files from Sources Folder to another location” and “Browse…” to that directory so that it shows up in the “Copy to Folder:” area of the dialog box. I also change the “Project URL:” so that it reads “http://localhost/~thcampbell/PhpDbTest”.
You’ll need to substitute your username where I’m using “thcampbell”
After making these changes the dialog box should look like this:
Clik here to view.

Project URL Modification
Click “Finish”
Now NetBeans is going to start up and automatically opens an “index.php” file for us and we’re going to add the following (highlighted) between “<?php” and “?>”
Clik here to view.

PHP Database Connectivity Code
What’s going on here is that we are setting up a couple of variables for connecting to our database:
$dbhost = ‘localhost’;
$dbuser = ‘root’;
$dbpass = ‘xxxxxx’;
You would, of course, want to change the ‘xxxxxx’ to your database password from when we hooked up MySQL for our Apple machine.
The following line will do the actual connection to our database and if there is an error it will quit and throw an error message:
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to mysql’);
The following line does a select query of “Host” and “User” from the “mysql.user” table in our database:
$result = mysql_query(“SELECT Host,User from mysql.user”);
The “mysql.user” table is always there and holds the information of users that have access to our database as we saw when we hooked up our database in an earlier post.
These lines will display the data to our browser when we run the program:
while($row = mysql_fetch_array($result)) {
echo $row['Host'] . ” ” . $row['User'];
echo “<br />”;
}
and finally, let’s be a good boy or girl and close our database connection:
mysql_close($conn);
So, let’s run it from NetBeans by right clicking on our Project Name “PhpDbTest” and clicking on “Run”. The default browser should start up and you should see a screen similar to the following:
Clik here to view.

PHP/MySQL Successful Test
If you see this screen then you’re done! You connected to the database without any problem.
However, maybe you see this screen:
Clik here to view.

PHP/MySQL Unsuccessful Test
It’s probably your username or password for your database that is incorrect. It is also possible you don’t have your PHP talking to MySQL properly but that’s fairly unlikely if you did the steps in this post.
If you can’t figure it out or something else went wrong post something in the comments section and maybe I or someone else can help.
I used information from the following sites for this posting
http://www.w3schools.com/PHP/php_mysql_select.asp
http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/connect-to-mysql-database.aspx
Image may be NSFW.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
