001 Database Connection
001 Database Connection
--------------------------------------------------------------------------------
EXAMPLE 1: CREATING DATABASE IN MY PHP ADMIN
--------------------------------------------------------------------------------
c:\XAMPP
c. Click on GO
Database : maestro2
--------------------------------------------------------------------------------
EXAMPLE 2: CONNECTING TO DATABASE USING PHP
-------------------------------------------------------------------------------
1. Make sure XAMPP is installed and the Apache & MySQL modules are running.
2. Create a database in phpMyAdmin (e.g., maestro).
3. Use the following PHP script to connect to the MySQL database. Using any
text editor, create the following PHP script.
4. This script checks if the connection is successful, and if not, it prints the
error message.
Filename : db_connect.php
---------------------------
<?php
// Database configuration
$host = "localhost"; // Hostname
$username = "root"; // MySQL username (default in XAMPP is 'root')
$password = ""; // MySQL password (default in XAMPP is empty)
$dbname = "maestro"; // The database name you created
// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully to the database";
}
?>
Filename : db_connect.php
Directory Path : C:\xampp\htdocs\PHP_EXER\CONNECT
--------------------------------------------------------------------------------
EXAMPLE 3: CREAD A TABLE IN maestro DATABASE
-------------------------------------------------------------------------------
1. Create dept table
a. Click SQL window and execute this create table command
--------------------------------------------------------------------------------
EXAMPLE 4: DISPLAY SIMPLE RECORD FROM MYSQL DATABASE
--------------------------------------------------------------------------------
Create a PHP program that will display the records from dept mysql table. The dept
table is created inside the maestro database in mysql database.
List of Records:
Filename :
<?php
// Database connection parameters
$servername = "localhost"; // Change this if your MySQL server is not local
$username = "root"; // Change this to your MySQL username
$password = ""; // Change this to your MySQL password
$dbname = "maestro"; // Database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "</table>";
} else {
echo "0 results";
}
--------------------------------------------------------------
JAVA JDBC
--------------------------------------------------------------