Skip to content
PHP Logo

How can an external user create a table with a password-protected access using PHP and MySQL?

To create a table with password-protected access in PHP and MySQL, you can use a combination of MySQL queries and PHP code. First, you will need to create a MySQL table with a column to store passwords. Then, you can use PHP to prompt the user for a password before granting access to the table.

php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Prompt user for password
$password = $_POST['password'];

// Check if password is correct
$sql = "SELECT * FROM users WHERE password = '$password'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // Password is correct, allow access to table
  $sql = "SELECT * FROM table_name";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
    // Output data from table
    while($row = $result->fetch_assoc()) {
      echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
    }
  } else {
    echo "0 results";
  }
} else {
  echo "Incorrect password";
}

$conn->close();
?>