0% found this document useful (0 votes)
18 views2 pages

Create A Database and Table in MySQL

The document provides instructions for creating a MySQL database and table, specifically a database named 'simpledb' and a table 'emp' with employee data. It also includes a PHP script to connect to the database and retrieve user data, displaying it in a web browser. Additionally, troubleshooting tips are provided for common issues related to MySQL credentials and Apache server status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Create A Database and Table in MySQL

The document provides instructions for creating a MySQL database and table, specifically a database named 'simpledb' and a table 'emp' with employee data. It also includes a PHP script to connect to the database and retrieve user data, displaying it in a web browser. Additionally, troubleshooting tips are provided for common issues related to MySQL credentials and Apache server status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Create a Database and Table in MySQL

Log into MySQL and create a simple database and table.

sudo mysql -u root –p


Once logged in, execute the following commands:
mysql$ CREATE DATABASE simpledb;
USE simpledb;
CREATE TABLE emp( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100)
);

DESC emp;
INSERT INTO emp (name, email) VALUES ('John Doe', '[email protected]');
INSERT INTO emp (name, email) VALUES ('Jane Doe', '[email protected]');
Select * from emp;

Exit MySQL:
Exit

Create a PHP Script to Connect to the Database:


Create a PHP file in your web server's root directory. By default, Apache serves files from
/var/www/html/.
sudo nano /var/www/html/db_connect.php

Add the following PHP code to connect to MySQL and retrieve data from the users table:

php
Copy
<?php
$servername = "localhost";
$username = "root"; // default MySQL root user
$password = "your_mysql_root_password"; // replace with your MySQL root
password
$dbname = "simpledb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, name, email FROM users";


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

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " .
$row["email"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

6. Test Your PHP Script

To test the script, open a web browser and navigate to:

arduino
Copy
http://localhost/db_connect.php

You should see the data from your users table displayed in the browser, such as:

bash
Copy
id: 1 - Name: John Doe - Email: [email protected]
id: 2 - Name: Jane Doe - Email: [email protected]

7. Troubleshooting

●​ If you see an error like Warning: mysqli::mysqli() [mysqli.mysqli]:


(HY000/1045): Access denied for user, it means there’s an issue with the MySQL
credentials. Make sure you enter the correct password for the MySQL root user.
●​ Ensure that Apache is running by checking the status with sudo systemctl status
apache2.

You might also like