Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get variables work beyond curly brackets

Tags:

variables

php

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']; 
?>
like image 771
bɪˈɡɪnə Avatar asked Dec 06 '25 01:12

bɪˈɡɪnə


1 Answers

<?php
 $id=""; //initialize a variable with blank value
 if (isset($_GET['username'])) {
 // query to get data
 $id = $_GET['id'];
 }
 else {
 // error
 }
?>
like image 88
Manish Gaikwad Avatar answered Dec 08 '25 15:12

Manish Gaikwad