0% found this document useful (0 votes)
23 views

001 Database Connection

Uploaded by

Ji Eun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

001 Database Connection

Uploaded by

Ji Eun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

================================================================================

TOPIC 1 : CONNECTING TO MYSQL DATABASE USING PHP


================================================================================

--------------------------------------------------------------------------------
EXAMPLE 1: CREATING DATABASE IN MY PHP ADMIN
--------------------------------------------------------------------------------

1. Open your XAMPP PHP ADMIN


a. Open your XAMPP folder

c:\XAMPP

b. Open the xampp-control.exe

2. Start the Apache and MySQL

a. Click the <START> button near Apache Server (Top line)


b. Click the <START> button near MS SQL Server (2nd to the Top line)

Note : It is running when that button turns into

3. Create new database name "maestro"

a. Click SQL Window

b. Execute this sql command

create database maestro;

c. Click on GO

Another way of creating a database


----------------------------------
a. Click on Server (status line) to close all database
b. Click on New
c. Type the database name and then click

Database : maestro2

4. Select the database

a. You may type this command in SQL window


USE maestro

b. Or, just click maestro in the database list (Left screen)

5. To delete a database, you can use drop database command.

drop 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";
}
?>

Executing the PHP Script


-------------------------
1. Save the file as db_connect.php in the htdocs folder of your XAMPP installation
(e.g., C:\xampp\htdocs on Windows).

Filename : db_connect.php
Directory Path : C:\xampp\htdocs\PHP_EXER\CONNECT

2. List of connection details.

$host: Hostname where your database is hosted (usually localhost in XAMPP).


$username: MySQL username (root by default).
$password: MySQL password (default is empty in XAMPP).
$dbname: The name of your database (replace with your actual database na

3. Open your browser and navigate to


http://localhost/PHP_EXER/CONNECT/db_connect.php.

4. If you see "Connected successfully to the database" in your browser, the


connection is successful!

--------------------------------------------------------------------------------
EXAMPLE 3: CREAD A TABLE IN maestro DATABASE
-------------------------------------------------------------------------------
1. Create dept table
a. Click SQL window and execute this create table command

CREATE TABLE dept (


deptno INT, -- Numeric value for department number
dname VARCHAR(50), -- String value for department name
loc VARCHAR(50) -- String value for location
);
CREATE TABLE dept (
deptno INT,
dname VARCHAR(50),
loc VARCHAR(50)
);

b. Another way of creating table

2. Insert new records into dept table

INSERT INTO `dept` VALUES ('10', 'ACCOUNTING', 'NEW YORK');


INSERT INTO `dept` VALUES ('20', 'RESEARCH', 'DALLAS');
INSERT INTO `dept` VALUES ('30', 'SALES', 'CHICAGO');
INSERT INTO `dept` VALUES ('40', 'OPERATIONS', 'BOSTON');

--------------------------------------------------------------------------------
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.

Database Name : maestro


Table Name : dept

Dept Table Structure:

DEPTNO INT (2) NOT NULL


DNAME VARCHAR (14) NOT NULL
LOC VARCHAR (13)

List of Records:

DEPTNO DNAME LOC


------ ------------- ---------------
10 'ACCOUNTING' 'NEW YORK'
20 'RESEARCH' 'DALLAS'
30 'SALES' 'CHICAGO'
40 'OPERATIONS' 'BOSTON'

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);
}

// SQL query to fetch records from dept table


$sql = "SELECT DEPTNO, DNAME, LOC FROM dept";
$result = $conn->query($sql);

// Check if there are any records


if ($result->num_rows > 0) {
// Display table header
echo "<table border='1' cellpadding='10' cellspacing='0'>";
echo "<tr><th>DEPTNO</th><th>DNAME</th><th>LOC</th></tr>";

// Output data for each row


while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["DEPTNO"] . "</td>";
echo "<td>" . $row["DNAME"] . "</td>";
echo "<td>" . $row["LOC"] . "</td>";
echo "</tr>";
}

echo "</table>";
} else {
echo "0 results";
}

// Close the database connection


$conn->close();
?>

CREATE TABLE login (


id VARCHAR(20),
username VARCHAR(20),
password VARCHAR(20)
);

insert into login values (1,'Bob', '123456');


insert into login values (2,'Adam','456789');
insert into login values (3,'Anna','123789');

--------------------------------------------------------------
JAVA JDBC
--------------------------------------------------------------

1. Create a project "Jdbc_Project"


2. Create a package "Jdbc_Package" under "Jdbc_Project"
3. In Libraries folder under the "Jdbc_Project", add the "MySQL JDBC Driver"
a. Right Click on Libraries
b. Click on "Add Libraries"
c. Select MySQL JDBC Driver" and then click <Add Library> button.

You might also like