0% found this document useful (0 votes)
6 views5 pages

Mysql

The document provides PHP code snippets for various MySQL database operations, including connecting to a database, creating a table, inserting, retrieving, updating, and deleting data. It also includes a simple login system and a method to count the number of records in the 'users' table. Each section demonstrates basic error handling and connection management.

Uploaded by

noyalaugusthy
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)
6 views5 pages

Mysql

The document provides PHP code snippets for various MySQL database operations, including connecting to a database, creating a table, inserting, retrieving, updating, and deleting data. It also includes a simple login system and a method to count the number of records in the 'users' table. Each section demonstrates basic error handling and connection management.

Uploaded by

noyalaugusthy
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/ 5

1.

Connect to MySQL Database

<?php

$servername = "localhost";

$username = "root";

$password = "";

$database = "test_db";

$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

echo "Connected successfully!";

$conn->close();

?>

2. Table creation

<?php

$conn = new mysqli("localhost", "root", "", "test_db");

$sql = "CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(50),

email VARCHAR(50)

)";

if ($conn->query($sql) === TRUE) {

echo "Table created successfully";

} else {

echo "Error: " . $conn->error;

}
$conn->close();

?>

3. Insert Data into Table

<?php

$conn = new mysqli("localhost", "root", "", "test_db");

$sql = "INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')";

if ($conn->query($sql) === TRUE) {

echo "Data inserted successfully";

} else {

echo "Error: " . $conn->error;

$conn->close();

?>

4. Retrieve Data from Table

<?php

$conn = new mysqli("localhost", "root", "", "test_db");

$sql = "SELECT * FROM users";

$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {

echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";

}
$conn->close();

?>

5. Update Data in Table

<?php

$conn = new mysqli("localhost", "root", "", "test_db");

$sql = "UPDATE users SET email='[email protected]' WHERE name='John Doe'";

if ($conn->query($sql) === TRUE) {

echo "Record updated successfully";

} else {

echo "Error: " . $conn->error;

$conn->close();

?>

6. Delete Data from Table

<?php

$conn = new mysqli("localhost", "root", "", "test_db");

$sql = "DELETE FROM users WHERE name='John Doe'";

if ($conn->query($sql) === TRUE) {

echo "Record deleted successfully";

} else {

echo "Error: " . $conn->error;

$conn->close();

?>
7. Create a Simple Login System

<?php

$conn = new mysqli("localhost", "root", "", "test_db");

$email = "[email protected]";

$password = "123456"; // Plaintext for demo purposes

$sql = "SELECT * FROM users WHERE email='$email'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

echo "Login successful!";

} else {

echo "Invalid credentials!";

$conn->close();

?>

8. Count Number of Records

<?php

$conn = new mysqli("localhost", "root", "", "test_db");

$sql = "SELECT COUNT(*) AS total FROM users";

$result = $conn->query($sql);

$row = $result->fetch_assoc();

echo "Total users: " . $row['total'];

$conn->close();
?>

You might also like