PDA

View Full Version : PHP and MySQL not talking


w19sms
05-13-2008, 02:53 PM
I'm new to PHP and MySQL but not to computers. I am currently taking a Web Publishing class at my local Community College and have installed PHP and MySQL as instructed via our textbook. I am running on XP Pro w/ SP2 and using IIS as my server. I installed IIS 1st, then MySQL, and finally PHP. I have set my php.ini up correctly, I borrowed a classmates php.ini file that is working correctly. I have been searching the forums for a while now and have not found anything close to my problem. I apologize if someone else has posted on this or I put this in the wrong forum.

Here is my problem:
I have a PHP script that does work correctly on another classmates server that I typed out of our textbook. Like I said the script works on other machines.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/zhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Register Diver</title>
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h1>Aqua Don's Scuba School Registration</h1>

<?php

if (empty($_GET['first_name']) || empty($_GET['last_name']) ||
empty($_GET['phone']) || empty($_GET['address']) ||
empty($_GET['city']) || empty($_GET['state']) ||
empty($_GET['zip']) || empty($_GET['email']))

exit("<p>You must enter values in all fields of
the New Diver Registration form! Click your browser's Back button
to return to the previous page.</p>");

echo "<p> a pre issue test</p>";
$DBConnect = @mysql_connect("localhost", "w19sms", "password")
Or die("<p>The database server is not available.</p>");
echo "<p>post issue test</p>";


$DBName = "scuba_school";
if (!@mysqli_select_db($DBConnect, $DBName)) {
$SQLstring = "CREATE DATABASE $DBName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
echo "<p>Successfully created the database.</p>";
mysqli_select_db($DBConnect, $DBName);
}


$TableName = "divers";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);


if (!$QueryResult) {
$SQLstring = "CREATE TABLE divers (diverID SMALLINT NOT
NULL AUTO_INCREMENT PRIMARY KEY, first VARCHAR(40),
last VARCHAR(40), phone VARCHAR(40),
address VARCHAR(40), city VARCHAR(40),
state VARCHAR(2), zip VARCHAR(10))";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to create the divers tale.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";
echo "<p>Successfully created the divers table.</p>";
}


$First = addslashes($_GET['first_name']);
$Last = addslashes($_GET['last_name']);
$Phone = addslashes($_GET['phone']);
$Address = addslashes($_GET['address']);
$City = addslashes($_GET['city']);
$State = addslashes($_GET['state']);
$Zip = addslashes($_GET['zip']);
$Email = addslashes($_GET['email']);


$SQLstring = "INSERT INTO divers VALUES(NULL, '$First', '$Last',
'$Phone', '$Address', '$City', '$State', '$Zip')";

$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";

$DiverID = mysqli_insert_id($DBConnect);

mysqli_close($DBConnect);


?>

</body>
</html>

When you run the html form and hit the register diver button it should run this script create the database and table the first time you hit the button and insert the information into the database.

What happens when I run it is I get the 1st echo line printed the "pre issue test" and that is all I get. PHP will not do anything else I do not get the "post issue test" or even the die message "The database server is not available.".

Why can I not get php to execute the following line?

$DBConnect = @mysql_connect("localhost", "w19sms", "password")
Or die("<p>The database server is not available.</p>");

I can access MySQL either by the MySQL Administrator Tool or command line access. All my accounts on MySQL have full access. I can create and modify databases in MySQL, I also can run queries and it will return the correct data.

If someone could point me in a better direction that would be great. I could really use some help, if anyone could please help me I would appreciate it.

Also I have sat with my professor for over an hour and we could not figure out what was going on.

TurboWebs
05-13-2008, 03:11 PM
I'm not familiar with using the Or command with the MySQL connect function to get the result you want. I would probably do:

$DBConnect = @mysql_connect("localhost", "w19sms", "password");

if (!$DBConnect)
die("<p>The database server is not available.</p>");

This would be my choice, I'm not saying your way is wrong, I'm just not familiar. I would check versioning, some things that you can do in PHP 5 won't work in PHP 4. And so on.

The other thing I noticed is that you are having a problem with the connection but you are still suppressing the errors. Stop suppressing the errors and you might get a better grasp of what is going on. The @ symbol is what tells PHP to suppress the errors. I would loose it so I could see the result since there's a problem. The @ symbol only needs to be there on production.

$DBConnect = mysql_connect("localhost", "w19sms", "password");

Next I would look into PHP and MySQL error logs to find out if additional information is written. If you go to your shell on this box can you log in with the following:

mysql -u w19sms -p

And then enter the password above when prompted? I would make sure that works as well.

Again I'm just throwing a bunch of stuff out to try. Without more information I can't get you any further.

w19sms
05-13-2008, 04:14 PM
Thanks for the quick response TurboWebs. I have tried all those suggestions already. I can connect to MySQL via command line. Basically the book tells you that you use the @mysqli_connect() with the or die command so you don't have to write an if statement. When I remove the @ from the mysqli_connect() it gives me a Fatal error: Call to undefined function mysql_connect()
I have checked double checked and I can not figure out why it is not reading the extensions correctly. I am not sure what else to try. I have put in the php_mysqli.dll into the ext folder in php and I have uncommented the extension in php.ini so if you or anyone could help me that would be great. I am at my wits end I'm about 2 weeks behind in my classwork and I am ready to scream at the top of my lungs. I'm just glad that I only need to have everything done by the end of the quarter.
Thanks for the help so far!!!!!

TurboWebs
05-13-2008, 04:31 PM
Well there is definitely your problem then. It appears that the MySQL functions haven't been installed with your PHP.

In your first message you mention mysql_connect() now you are talking about mysqli_connect()

What version of PHP are you running? The latter is only supported in PHP5. The former is supported in both. Which have you tried for sure?

What system are you running this all on? I've never had this issue with a system before, of course I normally just run Ubuntu server and it's pretty much already set up.

w19sms
05-13-2008, 05:11 PM
sorry for the confusion on that I'm running on a XP Pro machine I'm using IIS, PHP 5.2.5, and MySQL 5.0.1. I have tried downgrading to PHP 4.x.x and MySQL 4.x.x but with no success. I have been all over the net reading up on mysqli errors and how to fix it. I did not mean to jump around on you there.

Sorry about the code I was just trying mysql to see if it would work since mysqli was not. I forgot to fix the code before I submitted it. There was no success with mysql vs mysqli I still got the same error.

I used the msi installer to instal php. I also downloaded the zip file and pulled out the php_mysqli.dll file and put it in my C:\php\ext I went into my php.ini file and uncommented the "extension=php_mysqli.dll" line.

I read somewhere to put the libmysql.dll and php_mysqli.dll and place it in C:\Windows\System32 folder. I tried that but nothing happened to my advantage.

Could it be I'm just completely overlooking something very simple?

Thanks

TurboWebs
05-13-2008, 05:21 PM
AAAAHHH HAH

See there's your biggest problem you have Windows. It's like you are wanting to program for a LAMP installation (Linux Apache MySQL PHP) But instead you are running in a WIMP environment (Windows IIS MySQL PHP). I've never personally done this. I use Windows for my desktop but I go purely Linux for development and web.

I can tell you one thing though. Since almost everywhere you go, to program in MySQL and PHP you will also be on Linux/Apache I would highly recommend recreating this same environment to learn from.

I can't be of much help with your Windows PC, but if you want to simulate a LAMP environment on your Windows PC check out this:
XAMPP for Windows LAMP Development (http://www.apachefriends.org/en/xampp-windows.html)

This will completely simulate a LAMP installation on Windows. Works well too, I know a lot of developers that use it because they don't have development environments handy. I've tried it a few times with great success, but I normally keep a LAMP box sitting around for development. Extra crappy computers are easy to come by and work perfectly for LAMP.

I know you probably aren't wanting to embrace this alternative with open arms since you've spent so much time with IIS, but it's an alternative when all else fails. Me personally I would have never started with IIS.

Good Luck.

P.S. I would hate to be developing on anything called WIMP, blah. :) lol

w19sms
05-13-2008, 06:41 PM
Thanks TurboWebs for your help. If I had the time I would look into your alternative, but like I said this is for school and I'm trying use and follow what the textbook has. I have a server that is using Window Server 2003, Apache 2.2.8, PHP 5.2.5 and MySQL 5.0.1. It is not completely set up to work correctly right now. I wanted to get through this class before I went down that road. I'm not a big fan of IIS, I prefer Apache over IIS. Like I said though I'm doing this for class so I don't have much of a choice. I can go a different path but then I run into the things don't work right or they don't want to work together problem.

I had everything working 2 weeks ago, all I did was hibernate my laptop drive home from school showed my wife I got it working. The laptop got shut off, when I turned it back on the next time it had stopped working and left me where I am now.

But again thanks for your help TurboWebs

TurboWebs
05-13-2008, 06:55 PM
Okay well good luck. It's too bad you feel you are stuck into MS that much. I hear the same old thing everytime that getting PHP and MySQL to run on windows is a huge pain. Takes a lot to get it right. This is probably because PHP and MySQL are native Linux applications not Windows apps.

I can get XAMPP running on a Windows machine in about 5 seconds unless I am running on a slow internet connection and it takes a while to download for some reason. It actually creates a simulation Linux environment, and runs MySQL and PHP within that environment in which they run the best. And everything is already preconfigured to work. There is no "getting things to work right and don't want to work together".

It's to bad you don't have an extra windows PC around that you could spend 10 minutes on with XAMPP, you'd see it is really easy. And then for farther down the road if you want to use PHP and MySQL I would take an old computer throw it on your network and install Ubuntu on it. Put PHP and MySQL back where they belong, in Linux.