How to connect to a database ?
I have written a login page in PHP but how to connect to a database as I am not able to make my user to login to my page.
How can it be done?
I have written a login page in PHP but how to connect to a database as I am not able to make my user to login to my page.
How can it be done?
Hi alfred
Hello Alfred,
Check the following.
This must be the reason why you can’t connect to your database.
This is some simple guide to fix those error.
Hope this help.
Regards,
Elliottsims
I think there is a missing part in your login page PHP script that makes users unable to connect or login to your web page. In order for a user to connect to a database or access data from a database, you must first need to establish a connection to the database itself. Since you are using PHP, it can be done using the function “mysql_connect()”. Below is the function’s correct syntax.
Where servername is the name of the server you want to connect to. The default is localhost:3306. Below is an example of how to create a connection to the database using PHP.
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
die('Could not connect: ' . mysql_error());
}
// some code
?>
The connection automatically closes when the script ends. Or, if you want to close the connection before the script terminates, you may use the function “mysql_close()”. See below for the example.
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>