Tuesday, May 1, 2007

My PHP Finally Creates A Database

I finally created a database using PHP. I just followed the example closely:
<?php
$link = mysql_connect('localhost', 'root', '*****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

$sql = 'CREATE DATABASE dental_office';
if (mysql_query($sql, $link)) {
echo "Database dental_office created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
?>


Using a string variable to capture the connection tag makes it a lot easier to call in the future. Soon I will be able to create code that allows patients to fill out information online and sent it to the dental office immediately. (This happens to be a very big convenience as I work at a dental office where that is needed :p)

My code went off without a hitch. I even found there are ways to connect directly to specific ports and sockets by replacing localhost with the site's name or IP. I only have one server and one data base so I don't think I'll be needing that I'm happy to finally be on track. Variables are running well, I'm connecting to MySQL through PHP, I've even gotten PHPMyAdmin to create tables for me on the database.

Everything is looking up for Milhouse, er Jay.
Hey! I just realized this is my 10th post! I've also reached another official benchmark. My Blog appears across two months, April and May!

To celebrate, here is Champagne!

Creative Commons License, photo from FreePhotoBank.org

Well I don't drink, but even non-drinkers can appreciate the majesty of Champagne, France!

Learning PHP-MySQL: Trudging Along

In my search for code I entered mysql php test on my Google search bar and the first result was PHP/MySQL Tutorial - Part 1 by freewebmasterhelp.com. Feeling lucky (pun intended) I clicked through and upon reading was convinced that a simple <?php phpinfo(); ?> was all I needed to check if MySQL was working properly. The tutorial tells me if MySQL is mentioned on the page then it has been installed. I see MySQL and MySQLi and decide to stop right there. Then I decide to start up again.
I see Part 4 - Displaying Data and decide to give their code a test drive. I get error messages th
at I do not even want to go into so I am going to install PHPMyAdmin instead.
Must...procrastinate.....longer.
PHPMyAdmin is an administrative tool that makes handling MySQL easier.

After downloading PHPMyAdmin I unzipped the contents into c:/myserver and entered http://localhost/phpMyAdmin-2.10.1-all-languages-utf-8-only/scripts/setup.php in my web browser.

Look at all these wonderful toys.

So after going through Servers and adding my own with info I found on <?php phpinfo(); ?> I went back to the drawing board and started echoing examples from the official PHP Manual. I have been able to connect to MySQL with the following code which I derived from their section on mysql_connect()
<?php
//Example 1352. mysql_connect() example
//http://us.php.net/manual/en/function.mysql-connect.php
$user = "root";
$password= "*****";
$link = mysql_connect('localhost', $user, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
I am able to manipulate databases using PHPMyAdmin. Unfortunately, I still can't create a database using PHP. At least I know it is possible to connect.