1

I have a problem with PHP variables. I am showing username which user id is logged in with $_SESSION['uid'];.

Like $uid = $_SESSION['uid'];

and I have to get the username with

$data = $Details->UserDetails($uid);
$data_username = $data['username'];

So in the index page shows me <?php echo $data_username;?> but when i use the $data_username in the following code it is giving me error.

    <?php 

    include_once ("includes/datas.php"); 
    //session_start(); and $data_username will be come within datas.php
    if ($_GET['action'] == "get_all_posts") { get_all_posts($db,$config); }
    function get_all_posts($db,$config) {
     $sql = "select * from `posts` where ((
     to_uname = '".mysqli_real_escape_string($db, $data_username)."' AND 

     from_uname = '".mysqli_real_escape_string($db,$_GET['client'])."' ) OR (

     to_uname = '".mysqli_real_escape_string($db,$_GET['client'])."' AND 

     from_uname = '".mysqli_real_escape_string($db,$data_username)."' )) order by post_id DESC ";
    }
    ?>

I am getting Notice: Undefined variable: data_username What I am missing here anyone can tell me?

AlwaysStudent
  • 1,354
  • 18
  • 49
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Qirel Jun 02 '17 at 09:20
  • try to make an echo $data_username; under include_once, lets see if its there – Foo Bar Jun 02 '17 at 09:20
  • And it doesn't seem like you're running the query, or returning the query string either – Qirel Jun 02 '17 at 09:21

3 Answers3

2

pass $data_username in to the function ie,

if ($_GET['action'] == "get_all_posts") { get_all_posts($db,$config, $data_username); }
Praveen P K
  • 198
  • 12
0

To get your variable inside of your function you need to say to your function to use this variable. add global $data_usernameunder your `function get_all_posts($db,$config) {``

So the correct code is :

include_once ("includes/datas.php"); 
//session_start(); and $data_username will be come within datas.php
if ($_GET['action'] == "get_all_posts") { get_all_posts($db,$config); }
function get_all_posts($db,$config) {
 global $data_username; // Try to add this line
 $sql = "select * from `posts` where ((
 to_uname = '".mysqli_real_escape_string($db, $data_username)."' AND 

 from_uname = '".mysqli_real_escape_string($db,$_GET['client'])."' ) OR (

 to_uname = '".mysqli_real_escape_string($db,$_GET['client'])."' AND 

 from_uname = '".mysqli_real_escape_string($db,$data_username)."' )) order by post_id DESC ";
}
Fabien
  • 548
  • 7
  • 18
  • Usage of the global keyword isn't a good practice - it should be passed as an argument to the function instead – Qirel Jun 02 '17 at 09:28
0

Maybe u should figure out the concept of global variables and local variables. Which means u cannot use a variable in ur function that havent defined in it yet except the variable is global.

You can send the variable by parameter like this:

function get_all_posts($db,$config,$data_username)

or use the keyword 'global' inside your function to claim the variable like this:

global $data_username;

But always use objects while handling complicated data.

Separes
  • 11
  • 2