Is it better to create a new table for storing user details or to expand the existing user table with additional columns?
It is generally better to expand the existing user table with additional columns rather than creating a new table for storing user details. This approach helps to keep related data together, simplifies queries that involve multiple user attributes, and reduces the complexity of managing multiple tables.
// Example of expanding the existing user table with additional columns
$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);
}
// Alter the existing user table to add additional columns
$sql = "ALTER TABLE users ADD COLUMN email VARCHAR(255), ADD COLUMN phone VARCHAR(20)";
if ($conn->query($sql) === TRUE) {
echo "Table altered successfully";
} else {
echo "Error altering table: " . $conn->error;
}
$conn->close();
Related Questions
- In what scenarios would one need to detect the encoding of a string in PHP and how can this be achieved effectively?
- What are the advantages of using PHP sessions over JavaScript for managing shopping cart data in an online shop?
- How can PHP beginners ensure proper syntax and formatting when writing code, especially within forum discussions?