Sunday, April 29, 2007

Testing MySQL: Installing MySQL, configuring php.ini and removing deprecated tags.

Previously, I was testing MySQL and could not get a page to load at all. I was receiving the error message:
PHP Fatal error: Call to undefined function mysql_connect()

I added a couple extensions in php.ini and just now found a new error message appeared.
PHP Warning: PHP Startup: Unable to load dynamic library './php_mysql.dll'
- The specified module could not be found.\r\n in Unknown on line 0

It was not looking in the right directory, rather I never specified where to look. Now I have tried setting the extension directory with extension_dir = "c:\php\ext" in php.ini, I have received a new error message. (I'm getting somewhere!)
PHP Fatal error: Call to undefined function mysql_create_db() in
C:\\myserver\\test_mysql.php on line 33

I have a feeling I'll be doing a lot of php.ini configuring. After searching for the previous error in google I came across a list of emails in the archives of Neohapsis.com where someone mentioned that mysql_create_db() is deprecated. So much for using older tutorials. I searched for mysql_create-db() deprecated on Google and came across a comment on the manual that says mysql_query() should be used instead.

I went back into my test file and changed the two instances of mysql_create_db to mysql_query and finally something appears on the page. Unfortunately the variables are not appearing to be saved. Oh error looooog!,
PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource in C:\\myserver\\test_mysql.php on line 66

Fetch doesn't have anything to fetch so it is reporting an error. I think I'll find someone elses file to test MySQL with (ignores the little voice saying "Make one yourself you lazy oaf")

Testing MySQL


I decided to run a test on MySQL to see if it has been configured properly.




WebThang's tutorial has a test file to use on the end of its MySQL installation tutorial that should work fine (the only change I made was changing shorthand error):




Cut and Paste the following code into 'notepad' and save it as 'c:\web\test_mysql.php'

<?php
/************************************************************
this script runs only once, it then drops the database before
mysql connection is closed.
************************************************************/


/***************************************************
string values passed on to the following 5 variables.
two of each are assigned to 'ice' and the rest three
tag along into 'mysql_connect'
***************************************************/
$iceCreamOne = "Vanilla"; // Type One Ice Cream
$iceCreamTwo = "Cookie Dough Ice Cream"; // Type Two Ice Cream
$host = "localhost"; // hostname...in our case localhost
$username = "root"; // root is our default
$password = "ranginyoka31"; // your mysql password please


//open connection to the MySQL database server.
$connection = mysql_connect($host,$username,$password);
//if connection fails, display the error involved
if ($connection == false){
echo mysql_errno().": ".mysql_error()."<BR>";
//echo("Your username or password is not correct.");
exit;
}


/************************************************
After supper we will have two kinds of desserts
create the database then select it. Two functions
called mysql_create_db() & mysql_select_db()
if statements to check success of create & select
************************************************/
$create_success = mysql_create_db("aftersupper");
if($create_success)echo("<b><font color=\"blue\">create database: success!</font></b><br>");
$select_success = mysql_select_db("aftersupper");
if($select_success)echo("<b><font color=\"blue\">selected the created database: success!</font></b>");


/*************************************************************
your choice for 2 desserts
for me I like vanilla[iceCreamOne] & cookie dough[iceCreamTwo]
*************************************************************/
mysql_query("CREATE TABLE desserts(iceCreamOne VARCHAR(25),
iceCreamTwo VARCHAR(25))");


/**********************************************************
i put my two favorite ice cream types into table desserts: vanilla & cookie dough
remember im using variables that have been assigned with strings up there ^^^.
*********************************************************/
mysql_query ("INSERT INTO desserts (iceCreamOne, iceCreamTwo) VALUES
('$iceCreamOne', '$iceCreamTwo')");


/********************************************************
as long as there is information in the table keep printing.
i have two values in table 'desserts', both rows are passed to variable 'result'
**********************************************************/
$result = mysql_query ("SELECT * FROM desserts");


//checking to see that select was successfull
//if ($result){echo "<h2>Successfully selected from table desserts!</h2>\n";}


//assign the number of rows from variable $result to $numOfRows
//$numOfRows = mysql_num_rows ($result);


//for ($i = 0; $i < $numOfRows; $i++)
//{
$row = mysql_fetch_array($result);
print ("<h3>My 2 most favorite Ice Cream are:</h3><br>\n");
print($row["iceCreamOne"]." and ");
print($row["iceCreamTwo"]. "<br>");
// }


//Database gets dropped. You can comment the line below if you wish to keep the database
mysql_query("DROP DATABASE aftersupper");


//close the connection to the db with the particular user :: $username
mysql_close();
?>





So I tried to run the program and ended up getting a blank page. I looked at the MainFrame.err file and didn't see anything peculiar. I then looked at Apache's access log and found nothing.




The problem appeared in Apache's error log:


[Sun Apr 29 11:30:43 2007] [error] [client ###.#.#.#] PHP Fatal error: Call to undefined function mysql_connect() in C:\\myserver\\test_mysql.php on line 19




I searched for "PHP Fatal error: Call to undefined function mysql_connect()" on Google and found this thread on MySQL. At the top of the thread the first poster has the same problem as many others do. At the end of the thread the poster says the solution is located in the second to last post (a lot of pointing going on). That thread finally leads me to siteinaweek.com which finally makes me say "Oh, duh".




I forgot to add extensions for MySQL in php.ini


I just removed the semi-colin before "extension=php_mysql.dll" like so
and removed it before "extension=php_mysqli.dll" as well.
I stopped and restarted the Apache server before testing test_mysql.php and again received a blank page. I'll work on this some more later.

Saturday, April 28, 2007

Installing MySQL: Configuration

After installing MySQL I have to configure it. I have looked at both the MySQL Server Configuration Wizard Documentation and WebThang.co.uk's Tutorial for installing MySQL (I needed something a little less technical heavy to get me started and they've done a good job on tutorials).

When the window
this window
comes up I follow this sequence:
Next>Select Standard Configuration>Next>Next (Left Install As Windows Service and Launch the MySQL Server automatically on by default)>Entered my root password>Next>Execute>Finish

Now I test to see if it has been configured properly in good ol' Command Prompt
(Start>Programs>Accessories>Command Prompt)

I type in "c:/mysql/bin/mysql test" without the quotation marks on my prompt and...

then receive the following error message:
ERROR 1045 <28000>: Access denied for user 'ODBC'@'localhost'

I have no idea what this means. Going back to MySQL's "2.4.8.9. Starting the Server for the First Time" I see the suggestion to run mysqld.exe from the bin folder.

So I type in "c:/mysql/bin/mysqld. Instead of seeing a bunch of lines appear telling me something is being processed it quickly readies itself for the next prompt *blink blink*

Realizing my server is off I start Apache and attempt to run mysqld again. This time it waits for 5 or 6 seconds then readies itself for the next prompt without giving me any fanfare. It's supposed to give me fanfare. After reading 2.4.8.9 again I see that it will not show the lines I was expecting to see if I do not type "
--console" at the end of the command line. That last part was hidden in section navigation on the documentation page. Maybe they should make it a little more visible.

So I look in /mysql/data/MainFrame.err (it's where all the error messages and, if you don't enter --console, diagnostic messages go. Everything I expected to see in command prompt was there.

I logged into my account by changing the directory to c:/mysql/bin/
then entering mysql -u root password ****** in Command Prompt.

mysql - Mysql.exe, the program I'm opening
"-u" - means I'm about to enter the username
root - the ultimate grandmaster username upon installation
password - I entered a password on set up so I have to put password here if I'm going to access anything
****** - I can never tell (said in the voice of Jim Carrey's Riddler)

So I've got everything configured. Now...now...what am I going to do with all this stuff?

Installing MySQL

So I began by unzipping zip file into a folder of the same name. It really doesn't matter where it is unzippped. I now have setup.exe ready to run.

After it goes through a little song and dance preparing installation files and preparing to install it shows me a window, MySQL Server 5.0 - Setup Wizard

I click next> select custom (because I want to install in in c:/mysql instead of the default directory in Program Files)> click next> click change directory> enter c:/mysql under Folder name: > Click OK> click Next> click Install

After installation "Configure the MySQL Server Now" should be checked and I'll just click Finish and move on.

Hooray My MySQL is downloaded!

The moment of truth has arrived.

Before I even attempt installing it though I have to check the integrity of the file. To do this I am using md5.exe a program created by developed by Ron Rivest of the MIT Laboratory for Computer Science and RSA Data Security, Inc.

Md5 can validate the integrity of a downloaded file because the algorithm creates a different signature for two different files. If even one byte is changed in a file it will give a different signature. MySQL shows the signature id under the download link like so.


Hmm, I think I'll be uploading my pictures somewhere other than blogger. Is that blurred? Anyway, the second one is is what I downloaded. Below 5.037, 36.7M, etc there is a line that says MD5: b909c16... This is the line that md5.exe must return to show you have a file of pure integrity! (If only we could do the same for politicians, ahem) So the next step after downloading MySQL is to download MD5: Command Line Digest Utility.

After it has been downloaded it has to be unzipped (I'm assuming it was downloaded as a zip and not a tar. I use zips, never used tar). I simply unzipped it into c:/md5. I then placed the downloaded mysql-yada-yada-yada.zip in the c:/md5 folder along with md5.exe. Next we go to the command line.

In Microsoft XP, where they officially killed DOS, command prompt is used. It can be opened by following: Start>All Programs>Accessories>Command Prompt

If you've never used it don't worry it is actually quite simple (I used to have to use it every day because all I had was DOS long ago :-(!)

I started in the md5 directory but I wanted to show how change directory (cd\) works.
So I entered cd\ to return to c:\
Next I entered cd\md5 to change the directory to c:\md5
Then I entered md5 (it runs md5.exe) and entered the file name I want to check after it.
It returns a line, the signature for the downloaded file. I compare that line to the line on mysql's website and (no ellipse this time) they match!

On to installation!

Friday, April 27, 2007

MySQL Begins

I need to download MySQL 5.0 and it is 36.7 Megabytes in size *frown*. It wouldn't be so bad if I was back on campus or if I was able to download it at work. Unfortunately I'm not going to be on campus for awhile and the rewritable drive is busted at work. So I am going to have to use my dial-up from home to download the monster file.

Mysql-5.0.37-win32.zip from the redwire.net mirror is my choice of downloading because 1) I need the latest stable production of MySQL which is 5.0.x and 2) It happens to be the closes site to download from.

I don't know it it makes that much of a difference because it should take a few hours anyway. I'll just sleep on the download and hopefully wake up with MySQL ready to be set up.

test.php: There's long tags and then there's short tags.

This problem didn't take more than a few minutes to rectify and I'm thankful for that.

My test.php file consisted of the following code:
<? echo phpinfo() ?>
This is supposed to give you a volley of information about the PHP you just installed. Unfortunately when I tried to look at the file on localhost (http://localhost/test.php) it produced a blank window. I'm scratching my head thinking what did I do wrong?

After using Google (I can't say the term "googling" for fear that I'll mess with the giant's trademark. It's not good to say you "googled" something or "Xeroxed" a paper. They might lose money *rolls eyes* But seriously I like Google so I'll use the term "using Google" or "searching on Google") to search for
"GET /test.php HTTP/1.1" 200 20" "<? echo phpinfo() ?> "
I came up with only 6 hits that didn't answer the question as to why nothing was showing up. Then it hit me. Short tags.

The php.ini file has a variable that turns "short tags" on and off. Short tags are when you use . The ini file says it best:
; Allow the tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = Off
"short_open_tag" is the variable that is set to Off my default.
So all I had to do was replace <? with <?php and...
Voila!

PHP, The Eternal Struggle

I've always wanted to set up a dynamic site but whenever I tried to create a local server for testing I would fall flat on my face. For the last few weeks after browsing through the thousands of pages of the manuals for Apache 2.2, PHP, and MySQL 5.0 I tried to installing PHP to run on Apache.

The first snag I ran into (and I should have seen it at the beginning) was my attempt to install PHP 5.1 with Apache 2.2.

It turns out that PHP 5.1 does not have "php5apache2_2.dll" which is needed to run Apache as a module for enhanced security. After downloading PHP 5.2 and adding the lines
LoadModule php5_module "c:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"
I was finally able to start the Apache server without an error message. (c:/php was my choice of where I installed php. It might be different depending on where you unzipped your php file)

So I entered localhost and immediately became of forgetting a step. The Apache configuration file (httpd.conf) was still pointing at "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs" even though I had my root folder set up on the desktop. So I went into the file and found "DocumentRoot" where I promptly entered my desktop directory.

So I go to type in localhost and get the error message: 403 Forbidden You don't have permission to access / on this server. *frowns impatiently*

I go back into the httpd.conf file and scour through it line by line. Soon after DocumentRoot I encounter the problem:
# This should be changed to whatever you set DocumentRoot to.
#

About 26 lines after DocumentRoot it told me to change another line. So I pointed the directory to my desktop directory where all my files were located taking great care to use forward slashes (/) instead of backward slashes (\) and...

Htm files load perfectly but my test.php file comes up blank!