I went to several different tutorial sites for learning php/mySQL, and picked one of them. Well... intentionally or unintentionally, found few things were wrong with the sample codes given in the tutorial sessions. That prevented me to do copy & paste operations, and made me really dig into the stuff. I guess, after all, it was a good tutorial :O
Below is a code given in the tutorial session, which gets the data from the html forms and writes the data to the database.
<?php
$user="root";
$password="passpass";
$database="myfirstdatabase";
$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query);
mysql_close();
?>
Error 1:
When this code runs, there will be an error message in the browser.
The connection was reset
The connection to the server was reset while the page was loading.
The connection to the server was reset while the page was loading.
Windows also spits out the error message, saying:
"Apache HTTP Server has stopped working"
Problem signature:
Problem Event Name: APPCRASH
Application Name: httpd.exe
Application Version: 2.2.11.0
Application Timestamp: 493f5d44
Fault Module Name: php_mysql.dll
Fault Module Version: 5.3.0.0
Fault Module Timestamp: 4a492311
Exception Code: c0000005 ....
Problem Event Name: APPCRASH
Application Name: httpd.exe
Application Version: 2.2.11.0
Application Timestamp: 493f5d44
Fault Module Name: php_mysql.dll
Fault Module Version: 5.3.0.0
Fault Module Timestamp: 4a492311
Exception Code: c0000005 ....
Error 2:
And, there is another error message, which doesn't show up until the above error gets taken care of.
Notice: Use of undefined constant localhost - assumed 'localhost' in C:\wamp\www\testFolder\tetsuro\tetsuroL01_php_mySQL\insert.php on line 15
The Cause For Error 1:
The cause of error is mysql_close() not having MySQL link identifier. Although the php manual says that if MySQL link identifier is not specified, it will close the last opened link, it seems that since php 5.3, mysql_close() must have the link_identifier parameter.
The Cause For Error 2:
Missing quotation marks for localhost.
Wrong!
mysql_connect(localhost,$username,$password);
Correct.
mysql_connect("localhost",$username,$password);or,
mysql_connect('localhost',$username,$password);
Either " or ' will work.
Double or Single Quote?
Double-quoted strings are parsed.
Single-quoted strings are taken literally.
$str = 'His name is Joe';
$name = 'Joe';
$str = "His name is $name";
$name = 'Joe';
$str = "His name is $name";
Here's the updated working php script.
<?php
$user="root";
$password="passpass";
$database="myfirstdatabase";
$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
$link = mysql_connect('localhost',$user,$password);
if (!$link){
die('Could not connect: ' . mysql_error());
}
echo "connected";
echo "<br />";
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO contacts VALUES
('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query);
mysql_close($link);
echo "disconnected";
?>
No comments:
Post a Comment