0% found this document useful (0 votes)
29 views5 pages

PHP and MYSQL Database Connection

The document provides PHP code examples for connecting to a MySQL database, creating a database and table, inserting records, and displaying records from a table. It also explains sessions and cookies in PHP, detailing how to manage user information across multiple pages. Key functions such as mysqli for database operations and session_start() for managing sessions are highlighted.

Uploaded by

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

PHP and MYSQL Database Connection

The document provides PHP code examples for connecting to a MySQL database, creating a database and table, inserting records, and displaying records from a table. It also explains sessions and cookies in PHP, detailing how to manage user information across multiple pages. Key functions such as mysqli for database operations and session_start() for managing sessions are highlighted.

Uploaded by

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

PHP and MYSQL Database Connection

1)Check Database Connection


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

2) Create database in MYSQLi


<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>

3) Create Table in MYSQLi using PHP connection.


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to create table


$sql = "CREATE TABLE Emp (
Emp_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
salary INT(10))";

if ($conn->query($sql) === TRUE) {


echo "Table Employee created successfully";
}

else {
echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

4) Insert record into employee table using PHP


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Emp (firstname, lastname, email,salary)
VALUES ('Ankit', 'Dhobale', '[email protected]',45000)";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
}

else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

5) Insert multiple record into mysql using php


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO Emp (firstname, lastname, email, salary)


VALUES ('Kali', 'Dada', '[email protected]',40000);";

$sql .= "INSERT INTO Emp (firstname, lastname, email, salary)


VALUES ('Mariya', 'More', '[email protected]',45000);";

$sql .= "INSERT INTO Emp (firstname, lastname, email, salary)


VALUES ('Juli', 'Dhole', '[email protected]',48000)";

if ($conn->multi_query($sql) === TRUE) {


echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Note that each SQL statement must be separated by a semicolon.

6) Display the records of table.


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT Emp_id, firstname, lastname,salary FROM Emp";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>".
$row[“salary”];
}
}

else {
echo "0 results";
}
$conn->close();
?>

Notes:
1) Session:- When you work with an application, you open it, do some changes, and
then you close it. This is much like a Session. The computer knows who you are. It
knows when you start the application and when you end. But on the internet there is
one problem: the web server does not know who you are or what you do, because the
HTTP address doesn't maintain state.

Session variables solve this problem by storing user information to be used across
multiple pages (e.g. username, favorite color, etc). By default, session variables last until
the user closes the browser.

A session is started with the session_start() function.


Session variables are set with the PHP global variable: $_SESSION.

Example:-
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cow";
echo "Session variables are set.";
?>

</body>
</html>

2)Cookies:- A cookie is often used to identify a user. A cookie is a small file that the
server embeds on the user's computer. Each time the same computer requests a page
with a browser, it will send the cookie too. With PHP, you can both create and retrieve
cookie values.
A cookie is created with the setcookie() function.

You might also like