Are there any potential pitfalls to be aware of when creating a new database or table in PHP?
One potential pitfall to be aware of when creating a new database or table in PHP is not properly sanitizing user input, which can lead to SQL injection attacks. To prevent this, always use prepared statements or parameterized queries when interacting with the database.
// Example of using prepared statements to create a new table in PHP
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to create a new table with prepared statement
$stmt = $conn->prepare("CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), email VARCHAR(255))");
$stmt->execute();
echo "Table created successfully";
// Close connection
$conn->close();