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

MySQL PHP

Uploaded by

kibohe2968
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

MySQL PHP

Uploaded by

kibohe2968
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MySQL_EX_1_Creating_Connection

<?php
// when you install XAMPP on your computer, the password for the root
user is left empty.

$servername = "localhost";
$username = "root";
$password = "";

// mysqli_connect() function opens a new connection to the MySQL server.

// //The die() function is an alias of the exit() function.Print a


message and terminate the current script:

// mysqli_connect_error() function returns the error description from the


last connection error, if any.

//Creating a connection here with MySQL.


$conn = mysqli_connect($servername, $username, $password);

// I am Checking connection here.


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());

}
echo "Connected successfully";
?>
MySQL_EX_2_Creating_A_Database

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

//The mysqli_query() function accepts a string value representing a query


as one of the parameters and, executes/performs the given query on the
database.
// connection Required. Specifies the MySQL connection to use
// query Required. Specifies the SQL query string
// // mysqli_close() function closes a previously opened database
connection.

// I am Creating a connection here with MySQL.


$conn = mysqli_connect($servername, $username, $password);

// I am Checking connection here.


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// SQL qurey to Creating a database in MySQL.


$sql = "CREATE DATABASE School";

if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error! creating database: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
MySQL_EX_3_Creating_A_Table
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "School";

//Auto-increment allows a unique number to be generated automatically


when a new record is inserted into a table.

// I am Creating a connection here with MySQL.


$conn = mysqli_connect($servername, $username, $password, $dbname);

// I am Checking connection here.


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// SQL query to creating a table in (School) database.


$sql = "CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
stname VARCHAR(30) NOT NULL,
email VARCHAR(40),
mobile VARCHAR(10) NOT NULL
)";

if (mysqli_query($conn, $sql)) {
echo "Table students created successfully";
} else {
echo "Error! creating table: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
MySQL_EX_4_Inserting_Data_Into_Table
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "School";

// I am Creating a connection here with MySQL.


$conn = mysqli_connect($servername, $username, $password, $dbname);

// I am Checking connection here.


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// SQL query to inserting data in students table.

$sql = "INSERT INTO students (stname, email, mobile)


VALUES ('rohan', '[email protected]', '3322334456')";

if (mysqli_query($conn, $sql)) {
echo "New record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>
MySQL_EX_5_Selecting_Data

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

//mysqli_fetch_assoc() function fetches a result row as an associative


array.
//Returns an associative array of strings representing the fetched row.
NULL if there are no more rows in result-set
// I am Creating a connection here with MySQL.
$conn = mysqli_connect($servername, $username, $password, $dbname);

// I am Checking connection here.


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// SQL query to selecting all record from table


$sql = "SELECT id, stname, email, mobile FROM students";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) { // Returns the number of rows


in the result set
while($row = mysqli_fetch_assoc($result)) {
echo "<b>Id:</b> " . $row["id"]. ", <b>Name:</b> " . $row["stname"].
", <b>Email:</b> " . $row["email"]. ", <b>Mobile:</b> " .
$row["mobile"]. "<br>";
}
} else {
echo "no record found";
}

mysqli_close($conn);
?>
MySQL_EX_6_Where

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

// I am Creating a connection here with MySQL.


$conn = mysqli_connect($servername, $username, $password, $dbname);

// I am Checking connection here.


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

//SQL query to showing required record.

$sql = "SELECT id, stname FROM students WHERE mobile='1122334456'";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<b>Id:</b> " . $row["id"]. ", <b>Name:</b> " .
$row["stname"]. "<br>";
}
} else {
echo "no record found";
}

mysqli_close($conn);
?>
MySQL_EX_7_Updating

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

// I am Creating a connection here with MySQL.


$conn = mysqli_connect($servername, $username, $password, $dbname);

// I am Checking connection here.


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// SQL query to updating record.

$sql = "UPDATE students SET stname='Sunil' WHERE mobile='1122334456'";

if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error! updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
MySQL_EX_8_Deleting_Data

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

// I am Creating a connection here with MySQL.


$conn = mysqli_connect($servername, $username, $password, $dbname);

// I am Checking connection here.


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// SQL query to deleting record.

$sql = "DELETE FROM students WHERE id=1";

if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error! deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

You might also like