PHP Mysql
PHP Mysql
Note:
The connection string must be included in every script which intends to interact with the
database.
The connection string used is for a test environment and not a live server, therefore default
credentials are used.
connect.php
<?php
$sn = "localhost";
$un = "root";
$pass = "";
// Create connection
$conn = mysqli_connect($sn, $un, $pass);
// Check connection
if(!$conn)
{
die('Not Connected!' .mysqli_connect_error());
}
?>
PHP Create a MySQL Database
A database consists of one or more tables.
You will need special CREATE privileges to create or to
delete a MySQL database.
Create a MySQL Database
The CREATE DATABASE statement is used to create a
database in MySQL.
The following examples create a database named “y2023”
In this case we use connect.php on slide-4, note: no
database selection
<?php
//include connection string
include('connect.php');
//create database
$db="CREATE DATABASE y2023";
$db = “y2023";
// Create connection & select database
$conn = mysqli_connect($sn, $un, $pass, $db);
// Check connection
if(!$conn)
{
die('Not Connected!' .mysqli_connect_error());
}
?>
PHP Create table
<?php
//include connection
include('conn.php');
//create table
$tb="CREATE TABLE Student(
regno VARCHAR(17),
fname VARCHAR(20),
lname VARCHAR(20),
cw INT,
exam INT,
c_code VARCHAR(7),
PRIMARY KEY(regno)
)";
//extract records
$sel="SELECT * FROM Student";