In this code I have defined variable id now if I will use $id outside curly brackets I will get error undefined variable for $id. Is there any way in which I can use variables outside brackets.
<?php
if (isset($_GET['username'])) {
$check = $db->prepare("SELECT id, email, phone, FROM members WHERE username=:username");
$check->execute(array(':username'=>$username));
$get = $check->fetch(PDO::FETCH_ASSOC);
$id = $get['id'];
$email = $get['email'];
}
else {
// error
}
$posts =$db->prepare("SELECT * FROM posts WHERE id=:id");
$posts->execute(array(':id'=>$id));
$row=$posts->fetch(PDO::FETCH_ASSOC));
$title = $row['title'];
$body = $row['body'];
?>
<?php
$id=""; //initialize a variable with blank value
if (isset($_GET['username'])) {
// query to get data
$id = $_GET['id'];
}
else {
// error
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With