Connecting to MySQL, Querying Data, and Displaying Results with PHP
- ★ Type localhost in your web browser
- ★ Click phpMyAdmin, you will see a website that can deal with database.
- ★ Here is one tuotrial
to create databases, tables, and reset username and password in phpMyAdmin on XAMPP.
- ★ Here is another
PHP + MySQL
In this section, we will learn how to connect to a MySQL database, run SQL queries, and display the results using PHP.
- ★ Here is one example to open a MySQL database
- ★ In XAMPP, the servername is
localhost
, username is
root
and password is empty by default.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Connect to MySQL Database Server Using mysqli
// Object Oriented way
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
//Print a message and terminate the current script
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$conn->close(); //disconnect from the MySQL database
?>
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Create database
$sql = "CREATE DATABASE CSCI4410";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
- ★ Create Database in phpmyadmin
It is much easier to create databases and tables in phphmyadmin: tutorial
- ★ Usually, we connect to some MySQL database:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "CSCI4410"; //you need to create this database in PHPMyAdmin
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// write the SQL query
$sql = "SELECT * FROM Persons"; //select all the columns from database table Persons
//Perform query on a database
$result = mysqli_query($conn, $sql); //fetch the resulting rows as an associated array
$sql2 = "SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA ='CSCI4410' AND TABLE_NAME = 'Persons'"; // select all column names from table Persons
$columns = mysqli_query($conn,$sql2);
echo "<br>";
//Use table to display the database table
echo "<table border='1'>"; // table start
// table heading
echo "<tr>";
while($row = mysqli_fetch_assoc($columns))
{
foreach($row as $value)
echo "<th>" . $value . "</th>";
}
echo "<tr>";
// table rows
while ($row = mysqli_fetch_assoc($result)) //Get row from database table
{
echo "<tr>";
foreach ($row as $value) { // get the value for each row
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>"; // table end
$conn->close();
?>
persons.sql