How can values be retrieved from a database and passed as GET parameters in a PHP script?
To retrieve values from a database and pass them as GET parameters in a PHP script, you can first query the database to fetch the values, then construct a URL with the values as parameters, and finally redirect the user to the new URL with the parameters included.
<?php
// Connect to the 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);
}
// Retrieve values from the database
$sql = "SELECT column1, column2 FROM table WHERE condition";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$value1 = $row["column1"];
$value2 = $row["column2"];
// Redirect to new URL with values as GET parameters
header("Location: new_page.php?param1=$value1&param2=$value2");
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- In what ways can PHP forums or communities differentiate between beginner and advanced users to provide more targeted assistance and resources for each group?
- What potential issues can arise when using PHP to dynamically update content on a webpage?
- What are some alternative methods for finding differences in arrays in PHP besides array_diff?