How can PHP code be modified to display only the data of a specific user and not all data from a table?
To display only the data of a specific user and not all data from a table in PHP, you can modify the SQL query to include a WHERE clause that filters data based on a specific user identifier, such as a user ID or username. This way, only the data related to that specific user will be retrieved and displayed.
<?php
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// User ID of the specific user
$user_id = 1;
// SQL query to select data of a specific user
$sql = "SELECT * FROM table_name WHERE user_id = $user_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Data: " . $row["column_name"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Related Questions
- Are there any potential pitfalls in using variables to track user navigation in PHP?
- When generating random tokens for user verification in PHP, what are the considerations for choosing between functions like md5, uniqid, and random_bytes in terms of security and randomness?
- What best practices should be followed when configuring PHP extensions in a PHP 5 environment on IIS?