A simple tutorial to guide you on how to establish database connection using PHP to Mysql, Java to Mysql, and Asp.net to Mysql.
To establish a database connection, initially, you have to create the database and database users. Once done, you have to add users to the database. After completing all these preliminary process, you can establish the database connection using either PHP to Mysql or Java to Mysql or Asp.net to Mysql strings.
Step 1: Create database using cPanel or Plesk.
cPanel:https://www.shorttutorials.com/control-panel/creating-mysql-database.html
Plesk:https://www.shorttutorials.com/plesk/add-new-database.html
Step 2: Create the database users usingcPanel or Plesk.
cPanel:https://www.shorttutorials.com/control-panel/create-mysql-users.html
Plesk:https://www.shorttutorials.com/plesk/create-database-user.html
Step 3: Add users to the database created.
cPanel:https://www.shorttutorials.com/control-panel/add-user-to-database.html
Plesk:https://www.shorttutorials.com/plesk/add-new-user-to-databases.html
Step 4: Now, you can establish database connection in the following ways,
1. PHP to Mysql:
(i) For php version 5.6 and later
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
(ii) For php versions 5.4 and 5.5
<?php
$link = mysql_connect(localhost, mysql_user, mysql_password);
if (!$link) {
die(Could not connect: . mysql_error());
}
echo Connected successfully;
mysql_close($link);
?>
2. Java to Mysql
String url=jdbc:mysql://localhost:3306/myDBname?user=username&password=password;
con.in = DriverManager.getConnection(url);
3. Asp.net to Mysql
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
myConnectionString = "server=localhost;uid=username;" +
"pwd=12345;database=dbaname";
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = myConnectionString;
conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}