Getting data from the database depending on the value of the id in the URL.
$id= $_GET['id'];
$strSQL = "SELECT * FROM people WHERE id='$id'";
$rs = mysql_query($strSQL);
This is working, But it's displaying a blank page.
How can i fix this ?
解决方案
As I mentioned in my original comment:
"You need to use a loop in order to fetch the data, then echo it."
Others have given you examples and not to use your present method, here is a PDO method:
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';
$id = $_GET['id'];
try {
$pdo= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit( $e->getMessage() );
}
try {
$stmt = $pdo->prepare('SELECT * FROM people WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
/* optional method
foreach($stmt as $row) {
echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'];
}
*/
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$id = $row['id'];
$n = $row['name'];
$e = $row['email'];
}
echo $id;
echo "
";
echo $n;
echo "
";
echo $e;
?>
Another PDO method:
$mysql_hostname = 'xxx';
$mysql_username = 'xxx';
$mysql_password = 'xxx';
$mysql_dbname = 'xxx';
$id = $_GET['id'];
// $id= 4; // my own test id number
try {
$pdo= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
exit( $e->getMessage() );
}
try {
$sql = "SELECT * FROM people WHERE id='$id'";
$results = $pdo->query($sql);
} catch (PDOException $e) {
echo $e->getMessage();
}
/* optional method
foreach($results as $row) {
echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'];
}
*/
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$id = $row['id'];
$n = $row['name'];
$e = $row['email'];
}
echo $id;
echo "
";
echo $n;
echo "
";
echo $e;
?>