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

php_mysql

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

php_mysql

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

Connecting to MySQL database using PHP

Create an <?php
instance of the $servername = "localhost";
$username = "username";
mysqli class $password = "password";
providing all the
necessary details // Creating connection
required to $conn = new mysqli($servername, $username, $password);
establish the
connection such // Checking connection
as host, if ($conn->connect_error) {
username, die("Connection failed: " . $conn->connect_error);
password etc. }
echo "Connected successfully";
?>
10/07/2024 K R Parameshbalaji Sr. Software Faculty 91
Using MySQLi procedural procedure
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Creating connection
$conn = mysqli_connect($servername, $username, $password);

// Checking connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
10/07/2024 K R Parameshbalaji Sr. Software Faculty 92
Creating table using MySQLi
<?php $sql = "CREATE TABLE employees (
$servername = "localhost"; id INT(2) PRIMARY KEY,
$username = "username"; firstname VARCHAR(30) NOT NULL,
$password = "password"; lastname VARCHAR(30) NOT NULL,
$dbname = "newDB"; email VARCHAR(50)
)";
// Checking connection
$conn = mysqli_connect($servername, if (mysqli_query($conn, $sql)) {
$username, $password, $dbname); echo "Table employees created successfully";
// Check connection } else {
if (!$conn) { echo "Error creating table: " .
die("Connection failed: " . mysqli_error($conn);
mysqli_connect_error()); }
} mysqli_close($conn);
?>
// sql code to create table

10/07/2024 K R Parameshbalaji Sr. Software Faculty 93


Insert Record using MySQLi
<?php
$link = mysqli_connect("localhost", "root", "", "newdb");
if ($link == = false)
{
die("ERROR: Could not connect. ".mysqli_connect_error());
}

$sql = "INSERT INTO mytable (first_name, last_name, age) VALUES('ram', 'singh',


'25') ";

if (mysqli_query($link, $sql))
{
echo "Records inserted successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. ".mysqli_error($link);
}
mysqli_close($link);
?>
10/07/2024 K R Parameshbalaji Sr. Software Faculty 94
Selection in MySQLi
<?php echo "<td>".$row['Firstname']."</td>";
$link = mysqli_connect("localhost", "root", "", "Mydb"); echo "<td>".$row['Lastname']."</td>";
echo "<td>".$row['Age']."</td>";
if ($link === false) { echo "</tr>";
die("ERROR: Could not connect. " }
.mysqli_connect_error()); echo "</table>";
} mysqli_free_result($res);
}
$sql = "SELECT * FROM Data"; else {
if ($res = mysqli_query($link, $sql)) { echo "No matching records are found.";
if (mysqli_num_rows($res) > 0) { }
echo "<table>"; }
echo "<tr>"; else {
echo "<th>Firstname</th>"; echo "ERROR: Could not able to execute $sql. "
echo "<th>Lastname</th>"; .mysqli_error($link);
echo "<th>age</th>"; }
echo "</tr>"; mysqli_close($link);
while ($row = mysqli_fetch_array($res)) { ?>
echo "<tr>";
10/07/2024 K R Parameshbalaji Sr. Software Faculty 95
Create database
<?php
$dbhost = 'localhost’; $dbuser = 'root';
$dbpass = 'rootpassword‘ or ‘’;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{ die('Could not connect: ' . mysql_error()); }
echo 'Connected successfully';
$sql = 'CREATE Database test_db';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{ die('Could not create database: ' . mysql_error()); }
echo "Database test_db created successfully\n"; mysql_close($conn);
?>

10/07/2024 K R Parameshbalaji Sr. Software Faculty 96


Selecting Database
<?php
$conn = mysql_connect(‘localhost’, ‘root’, ‘’);
if(! $conn )
{ die('Could not connect: ' . mysql_error()); }
$db = mysql_select_db(‘test_db’);
mysql_close($conn);
?>
10/07/2024 K R Parameshbalaji Sr. Software Faculty 97
Creating Table
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{ die('Could not connect: ' . mysql_error()); }
mysql_select_db('test_db');
$sql = CREATE TABLE employee( emp_id INT NOT NULL AUTO_INCREMENT, emp_name
VARCHAR(20) NOT NULL, emp_address VARCHAR(20) NOT NULL, emp_salary INT NOT
NULL, join_date timestamp(14) NOT NULL, primary key ( emp_id ));
$retval = mysql_query( $sql, $conn );
if(! $retval )
{ die('Could not create table: ' . mysql_error()); }
echo "Table employee created successfully\n"; mysql_close($conn);
?>

10/07/2024 K R Parameshbalaji Sr. Software Faculty 98


Deleting Database
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) { die('Could not connect: ' . mysql_error()); }
$sql = 'DROP DATABASE test_db';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{ die('Could not delete database db_test: ' . mysql_error()); }
echo "Database deleted successfully\n"; mysql_close($conn);
?>

10/07/2024 K R Parameshbalaji Sr. Software Faculty 99


Insert Data into MySQL database
$conn = mysql_connect(‘localhost’, ‘root’, ‘rootpasswd’);
if(! $conn )
{ die('Could not connect: ' . mysql_error()); }
$sql = “INSERT INTO employee (emp_name,emp_address, emp_salary,
join_date) VALUES ( "guest", "XYZ", 2000, NOW() )”;
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{ die('Could not enter data: ' . mysql_error()); }
echo "Entered data successfully\n"; mysql_close($conn);
?>

10/07/2024 K R Parameshbalaji Sr. Software Faculty 100


Retrieving Data
<?php while($row = mysql_fetch_array($retval))

if(! $conn ) { echo "EMP ID :{$row[0]} <br> ".

{ die('Could not connect: ' . mysql_error()); } "EMP NAME : {$row[1]} <br> ".

$sql = 'SELECT emp_id, emp_name, emp_salary FROM "EMP SALARY : {$row[2]} <br> “;
employee';
} mysql_free_result($retval);
mysql_select_db('test_db');
echo "Fetched data successfully\n";
$retval = mysql_query( $sql, $conn );
mysql_close($conn);
if(! $retval )
?>
{ die('Could not get data: ' . mysql_error()); }

10/07/2024 K R Parameshbalaji Sr. Software Faculty 101


Updating Data
<?php
$link = mysqli_connect("localhost", "root", "", "Mydb");
if($link === false){
die("ERROR: Could not connect. ". mysqli_connect_error());
}
$sql = "UPDATE data SET Age='28' WHERE id=201";
if(mysqli_query($link, $sql)){
echo "Record was updated successfully.";
} else {
echo "ERROR: Could not able to execute $sql. ". mysqli_error($link);
}
mysqli_close($link);
?>
10/07/2024 K R Parameshbalaji Sr. Software Faculty 102
Deleting Data
<?php
$link = mysqli_connect("localhost", "root", "", "Mydb");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "DELETE FROM Data WHERE ID=201";
if(mysqli_query($link, $sql)){
echo "Record was deleted successfully.";
}
else{
echo "ERROR: Could not able to execute $sql. ". mysqli_error($link);
}
mysqli_close($link);
?>
10/07/2024 K R Parameshbalaji Sr. Software Faculty 103
MySQL backup
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{ die('Could not connect: ' . mysql_error());}
$table_name = "employee";
$backup_file = "/employee.sql";
$sql = "SELECT * INTO OUTFILE '$backup_file' FROM $table_name";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{ die('Could not take data backup: ' . mysql_error());
}
echo "Backedup data successfully\n";
mysql_close($conn);
?>
10/07/2024 K R Parameshbalaji Sr. Software Faculty 104

You might also like