7. PHP and MySQL
7. PHP and MySQL
---------------OR -----------------
<?php
// Create connection
$conn = mysqli_connect("localhost", "username", "password",
"myDB"); // Check connection
if (!$conn)
{
die("Connection failed: " .
mysqli_connect_error());
}
?>
Connection to MySQL
Parameter Description
host / servername Specifies a host name or an IP address
username Specifies the MySQL username
password Specifies the MySQL password
dbname Specifies the default database to be used
The mysqli_connect() function opens a new
mysqli_connect connection to the
MySQL server.
The die() function prints a message and exits the
die()
current script.
The mysqli_connect_error() function returns the
mysqli_connect_error(
error description
)
from the last connection error, if any.
Close the Connection
Example:
mysqli_close($conn);
Close the Connection
<?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());
}
mysqli_close($conn);
?>
Insert Data From a Form Into a Database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDbase";
// Create connection
$conn=mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
Insert Data From a Form Into a Database
CONT……
if (isset($_REQUEST['submit']))
{
extract($_REQUEST);
$IDNumber=mysqli_real_escape_string($conn,
$_POST['txtIDNumber']);
$StudName=mysqli_real_escape_string($conn,
$_POST['txtStudName']);
$Grade = mysqli_real_escape_string($conn,$_POST['txtGrade']);
mysqli_real_escape_string
- The mysqli_real_escape_string() function escapes special
characters in a string for use in an SQL statement.
Syntax:
mysqli_real_escape_string(connection,escapestring);
connection
- Specifies the MySQL connection to use
escapestring
- The string to be escaped. Characters encoded are NUL
(ASCII 0), \n, \r, \, ', ", and ControlZ.
Insert Data From a Form Into a Database
mysqli_query
The mysqli_query() function performs a query against the
database
Syntax:
mysqli_query(connection,query);
connection
- Specifies the MySQL connection to use
query
- Specifies the query string
Display the Result in an HTML Table
dislaystud.php
<?php
//DATABASE CONNECTION
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDbase";
// Create connection
$conn=mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
Display the Result in an HTML Table
dislaystud.php cont…
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['IDNumber'] . "</td>";
echo "<td>" . $row['StudName'] . "</td>";
echo "<td>" . $row['Grade'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
Deleting Data from a Database
deletestud.php cont…
<?php
//DATABASE CONNECTION
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDbase";
// Create connection
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
Deleting Data from a Database
deletestud.php cont…
mysqli_close($conn);
?>
Updating Data from a Database
updatestud.php
cont…
<?php
//DATABASE CONNECTION
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDbase";
// Create connection
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
Updating Data from a Database
updatestud.php
cont…
{
echo 'Student Successfully Updated';
}
mysqli_close($conn);
?>