How to create a connection with MySQL?
Use
How to connect to database?
Use mysqli_select_db("databasename", "connection");
How to insert data into MySQL database using PHP?
We can run MySQL query using mysqli_query
Use :
Please check the following code
$host = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($host, $username, $password) or die("Did not connect to MySQL");
mysqli_select_db($conn, "testdb") or die("Did not connect to db");
$query = "INSERT INTO users SET `firstname`='Gurpal', `lastname`='Singh', `email`='[email protected]'";
mysqli_query($conn, $query);
?>
How to update data into MySQL database using PHP?
We can run MySQL query using mysqli_query
Use :
Data before running update query
$host = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($host, $username, $password) or die("Did not connect to MySQL");
mysqli_select_db($conn, "testdb") or die("Did not connect to db");
$query = "UPDATE users SET `email`='[email protected]' WHERE id=3";
mysqli_query($conn, $query);
?>
Data after running above query
HOW TO SHOW THE DATA FROM MySQL TABLE
SELECT query is used to fetch data from database table.
e.g.
If we want to fetch values of single field of a table, we write query as
Select field1 from tablename
OUTPUT WILL BE
If we want to fetch values of multiple fields of a table, we write query as
Select field1, field2 from tablename
OUTPUT WILL BE
Leave a comment