Sunday, April 29, 2007

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.

No comments: