0% found this document useful (0 votes)
73 views6 pages

Module 4

MySQL is a popular and free database system that is used with PHP to power dynamic websites and web applications. PHP can connect to a MySQL database to perform operations like creating databases and tables, inserting, updating, selecting, and deleting data from MySQL tables. The document outlines the basic steps for connecting to a MySQL database using PHP and performing common CRUD operations.

Uploaded by

Mohd Kaif
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)
73 views6 pages

Module 4

MySQL is a popular and free database system that is used with PHP to power dynamic websites and web applications. PHP can connect to a MySQL database to perform operations like creating databases and tables, inserting, updating, selecting, and deleting data from MySQL tables. The document outlines the basic steps for connecting to a MySQL database using PHP and performing common CRUD operations.

Uploaded by

Mohd Kaif
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/ 6

PHP & MySQL

MySQL is the most popular database system used with PHP.

What is MySQL?

 MySQL is a database system used on the web


 MySQL is a database system that runs on a server
 MySQL is ideal for both small and large applications
 MySQL is very fast, reliable, and easy to use
 MySQL uses standard SQL
 MySQL compiles on a number of platforms
 MySQL is free to download and use
 MySQL is developed, distributed, and supported by Oracle
Corporation

Following steps are there:

 Connecting to MySQL database − Learn how to use PHP to open and


close a MySQL database connection.
 Create MySQL Database Using PHP  − This part explains how to
create MySQL database and tables using PHP.
 Delete MySQL Database Using PHP − This part explains how to
delete MySQL database and tables using PHP.
 Insert Data To MySQL Database − Once you have created your
database and tables then you would like to insert your data into
created tables. This session will take you through real example on
data insert.
 Retrieve Data From MySQL Database  − Learn how to fetch records
from MySQL database using PHP.
 Updating Data Into MySQL Database  − This part explains how to
update existing records into MySQL database using PHP.
 Deleting Data From MySQL Database  − This part explains how to
delete or purge existing records from MySQL database using PHP.
1. Open a Connection to MySQL
Before we can access data in the MySQL database, we need to be able to
connect to the server:

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

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

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";
?>

2. Close the Connection

The connection will be closed automatically when the script ends. To close
the connection before, use the following:

Example
$conn->close();

3. Create a MySQL Database Using MySQLi


Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database: "

4. PHP Create MySQL Tables

The CREATE TABLE statement is used to create a table in MySQL.

CREATE TABLE MyGuests (


id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)

 NOT NULL - Each row must contain a value for that column, null
values are not allowed
 DEFAULT value - Set a default value that is added when no other
value is passed
 UNSIGNED - Used for number types, limits the stored data to positive
numbers and zero
 AUTO INCREMENT - MySQL automatically increases the value of the
field by 1 each time a new record is added
 PRIMARY KEY - Used to uniquely identify the rows in a table. The
column with PRIMARY KEY setting is often an ID number, and is
often used with AUTO_INCREMENT

5. INSERT DATA INTO TABLES


The INSERT INTO statement is used to add new records to a MySQL table:

INSERT INTO table_name (column1, column2, column3,...)


VALUES (value1, value2, value3,...)

EXAMPLE

"INSERT INTO MyGuests (firstname, lastname, email)


VALUES ('John', 'Doe', 'john@example.com')"

6. Select Data From a MySQL Database


The SELECT statement is used to select data from one or more tables:

SELECT column_name(s) FROM table_name

or we can use the * character to select ALL columns from a table:

SELECT * FROM table_name

Example
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
mysqli_close($conn);
?>

7. DELETE Command
The DELETE statement is used to delete records from a table:

DELETE FROM table_name


WHERE some_column = some_value

Example :
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

8. UPDATE

The UPDATE statement is used to update existing records in a table:

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 

Example
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy