问题
I have created this simple PHP code for a login page
<head>
<title>Login Page</title>
</head>
<body>
<form method=POST>
<table>
<tr><th>UserName</th>
<td><input type=text name=user></input></td></tr>
<tr><th>Password</th>
<td><input type=password name=pass></input></td></tr>
<tr><th>Phone Number</th>
<td><input type=text name=phone></input></td></tr>
<tr><th>Email</th>
<td><input type=text name=email></input></td></tr>
<tr><th>City</th>
<td><input type=text name=city></input></td></tr>
<tr><td><input type=submit name=submit value=Submit></input></td></tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && !empty($_POST['user']))
{
if(preg_match("/^[a-zA-Z ]{3,40}$/",$_POST['user']))
{
if(preg_match("/^[a-zA-Z0-9]{8,20}$/",$_POST['pass']))
{
if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
if(preg_match("/[0-9]{10}/",$_POST['phone']))
{
$username=$_POST['user'];
$password=$_POST['pass'];
$Email=$_POST['email'];
$phoneNumber=$_POST['phone'];
$con=mysqli_connect("localhost:8080","root","","user");
mysqli_query($con,"insert into user values('$username','$Email','$phoneNumber','$city')");
}
else
{
echo "Invalid Phone number";
}
}
else
{
echo "Invalid Email";
}
}
else
{
echo "Invalid Phone number";
}
}
else
{
echo "Invalid user";
}
}
?>
Whenever i run this script and try to insert values into the tables, it takes a lot of time and throws the following error/Warning. I have not changed the username and password and i believe by default it is root and blank.
Warning: mysqli_connect(): MySQL server has gone away in C:\xampp\htdocs\home.php on line 39
Warning: mysqli_connect(): Error while reading greeting packet. PID=4708 in C:\xampp\htdocs\home.php on line 39
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\xampp\htdocs\home.php on line 39
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\home.php on line 39
Could someone help me with this as i believe that the problem lies with some of the port configuration.
Thanks and regards.
回答1:
By default, connecting to DB using XAMPP doesn't require you to specify the port. Replace localhost:8080
with localhost
回答2:
yo can increase Maximum execution time if you sure that your code is correct. this time is in php.ini config file
; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 30
回答3:
Below is the edited script. Try to run that. Also make sure mysql is working properly. Login Page UserName Password Phone Number Email City
</body>
</html>
<?php
set_time_limit (0);
if(isset($_POST['submit']) && !empty($_POST['user']))
{
if(preg_match("/^[a-zA-Z ]{3,40}$/",$_POST['user']))
{
if(preg_match("/^[a-zA-Z0-9]{8,20}$/",$_POST['pass']))
{
if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
if(preg_match("/[0-9]{10}/",$_POST['phone']))
{
$username=$_POST['user'];
$password=$_POST['pass'];
$Email=$_POST['email'];
$phoneNumber=$_POST['phone'];
$con=mysqli_connect("localhost:8080","root","","user");
mysqli_query($con,"insert into user values('$username','$Email','$phoneNumber','$city')");
}
else
{
echo "Invalid Phone number";
}
}
else
{
echo "Invalid Email";
}
}
else
{
echo "Invalid Phone number";
}
}
else
{
echo "Invalid user";
}
}
?>
回答4:
you don't have to specify the port number 8080 along with the localhost. simply use localhost instead
来源:https://stackoverflow.com/questions/25589473/error-connecting-to-mysql-in-xampp