Jump to content

PHP PDO variable not working in query but static value works


Go to solution Solved by kicken,

Recommended Posts

so my situation is something like this , i'm trying to fetch user details based on `id` that isset is getting, but some how the `variable that contains the $_GET value doesn't work` in query but when i put an static value to pdo query then it works and show the result. i have checked by doing `var_dump` of variable `$user` before query and it shows the correct value but not working in query. Below is the code i'm working with:

 



    public function profile_view($user_id = null) {
                $user = $user_id;
                $stmt = $this->_db->prepare('SELECT memberID,username,email,profile_pic,active FROM members WHERE memberID = :user_id AND active="YES"');
                $stmt->execute(array(':user_id'=>$user));
                while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $user_det = (object) array('username'=> $row['username'],'email'=>$row['email'],'profile_pic'=>$row['profile_pic'],'id'=> $row['memberID'],'active'=>$row['active']);
                
                return $user_det;
                }
                
            
        }


this is how function is being called (profile_view function is child of User class so $user is class User) :

 

   


 $view_profile = $user->profile_view($_GET['u']);
    

 

the above code returns null but when i put static value : `5` at the place of `$user` in `$stmt->execute` it returns the whole user's details which is what i need , but its not working with variable which is confusing me alot , thanks in advanced for help.

Your function parameter is called “user_id”, but inside the function, you try to access the variable “user” (without the “_id”). You need to decide which name to use.

 

 

inside function i'm declaring that  $user = $user_id;   and this does not seems to be connected the problem at all

Edited by ArshSingh

Are you sure it returns a 'null' value or could it possibly be 'False'?  Code looks good to me, just thinking that there is a problem with the query itself or that there are no rows returned.  Echo out your get var before the call and then in your function check if there are any rows returned by the query.

Are you sure it returns a 'null' value or could it possibly be 'False'?  Code looks good to me, just thinking that there is a problem with the query itself or that there are no rows returned.  Echo out your get var before the call and then in your function check if there are any rows returned by the query.

When i put a static value to : 

$stmt->execute(array(':user_id'=>$user));   ->  $stmt->execute(array(':user_id'=>'5'));

   it works and returns the result , and this is what i'm submiting to the function profile_view() , and i have also done _ var_dump of $user before query and it shows me the correct vaulue of what $_GET is getting from url.

Did you notice the key difference in your two statements?

And - you didn't answer my questions.

 

 

when using $user it returns "NULL" but when i put static value '5' it returns the array with all details.

 

and what do you mean with : Did you notice the key difference in your two statements?

Edited by ArshSingh

In one of them you pass a string value; in the other I'm assuming you pass a numeric value.

 

As for 'it returns "NULL"', that's odd.  Either it should return something from the query results, or if there are no results (which I asked you to check for me) it should return an undefined value since $user_det will not be defined.

 

So - once again - show the number of rows returned from the query to confirm that it actually runs. 

 

PS - What is this 'object' you are returning?  Isn't it just an array?  Why return this array instead of the array from the query?

In one of them you pass a string value; in the other I'm assuming you pass a numeric value.

 

As for 'it returns "NULL"', that's odd.  Either it should return something from the query results, or if there are no results (which I asked you to check for me) it should return an undefined value since $user_det will not be defined.

 

So - once again - show the number of rows returned from the query to confirm that it actually runs. 

 

PS - What is this 'object' you are returning?  Isn't it just an array?  Why return this array instead of the array from the query?

 

if return $user_det is not inside while loop and no result comes from query it gives me undefined $user_det variable error but with an static number value '5' it works smoothly , i'm returning this array as i'm working on different structure and i don't want to use : $row['username] something like this. i'm converting the array to object as i need the array to work in the following way : $user->username  not like this : $user[0]['username'] , it's just part of my program structure , including and excluding everything we come back to the odd problem : why it is reacting in that way , and not returning and result if used variable at the place of static number value.

Ok - a var dump of $user before the query is not what we want to see.

 

Good luck.  Can't seem to communicate with you.

Well sorry if i'm not able to explain the situation a bit more in detail , as i too don't have any other clue of what is happening. Anyway thanks for the help :)

 

and this is a result of query after putting static number value in query : 

 

object(stdClass)#7 (5) { ["username"]=> string(5) "admin" ["email"]=> string(15) "admin@gmail.com" ["profile_pic"]=> string(14) "1419529017.jpg" ["id"]=> string(1) "5" ["active"]=> string(3) "Yes" Edited by ArshSingh

Can you show us that result?

 

this is a var_dump of query :

object(stdClass)#7 (5) { ["username"]=> string(5) "admin" ["email"]=> string(15) "admin@gmail.com" ["profile_pic"]=> string(14) "1419529017.jpg" ["id"]=> string(1) "5" ["active"]=> string(3) "Yes"

I want you to do a var_dump() of the value of $user_id in this function (method). Something like:

 public function profile_view($user_id = null) {
                // here 
                var_dump($user_id); exit; 
                $user = $user_id;
                $stmt = $this->_db->prepare('SELECT memberID,username,email,profile_pic,active FROM members WHERE memberID = :user_id AND active="YES"');
                $stmt->execute(array(':user_id'=>$user));
                while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $user_det = (object) array('username'=> $row['username'],'email'=>$row['email'],'profile_pic'=>$row['profile_pic'],'id'=> $row['memberID'],'active'=>$row['active']);
                
                return $user_det;
                }
                
            
        }

// or 
public function profile_view($user_id = null) {
// here
var_dump($user_id);

$user = $user_id;
// and here
var_dump($user); exit;

$stmt = $this->_db->prepare('SELECT memberID,username,email,profile_pic,active FROM members WHERE memberID = :user_id AND active="YES"');
$stmt->execute(array(':user_id'=>$user));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$user_det = (object) array('username'=> $row['username'],'email'=>$row['email'],'profile_pic'=>$row['profile_pic'],'id'=> $row['memberID'],'active'=>$row['active']);

return $user_det;
}


}

PS: Copy / paste the exact output from what you're getting in the document / html page.

Edited by jazzman1

I want you to do a var_dump() of the value of $user_id in this function (method). Something like:

 public function profile_view($user_id = null) {
                // here 
                var_dump($user_id); exit; 
                $user = $user_id;
                $stmt = $this->_db->prepare('SELECT memberID,username,email,profile_pic,active FROM members WHERE memberID = :user_id AND active="YES"');
                $stmt->execute(array(':user_id'=>$user));
                while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $user_det = (object) array('username'=> $row['username'],'email'=>$row['email'],'profile_pic'=>$row['profile_pic'],'id'=> $row['memberID'],'active'=>$row['active']);
                
                return $user_det;
                }
                
            
        }

PS: Copy / paste the exact output from what you're getting in the document / html page.

 

 

this is what i get on var_dump of $user_id 

string(1) "5"

Now, it should work properly.

 as always result is "NULL"  there is a user with id : 5  , but it returns NULL with variable value , and with static number in execute() it resutnr the result with user details (this is what need to come as result).

Edited by ArshSingh

 

it returns NULL with variable value

 

The above output is 5. It should be integer, but anyway ;) Can you do a var_dump() of the GET variable? Do you have an empty space before or after 5 in the form input field? That's way I wanted to do var_dump(). 

The above output is 5. It should be integer, but anyway ;) Can you do a var_dump() of the GET variable? Do you have an empty space before or after 5 in the form input field? That's way I wanted to do var_dump(). 

 

i'm not getting the $_GET from an input , it's directly from url in this way :

 

locahost/easy/u/5

also the output of $_GET is :

string(1) "5"

 

i'm not getting the $_GET from an input , it's directly from url in this way :

That doesn't really matter. The output is string(1) "5". Do you get the same output to those 2 variables:

public function profile_view($user_id = null) {
// here
var_dump($user_id);

$user = $user_id;
// and here
var_dump($user); exit

...............................
}

 

That doesn't really matter. The output is string(1) "5". Do you get the same output to those 2 variables:

public function profile_view($user_id = null) {
// here
var_dump($user_id);

$user = $user_id;
// and here
var_dump($user); exit

...............................
}

 

 

yes i do get same value for those variables

Can I see the output of:

SHOW FULL COLUMNS FROM members

Run this statement via your mysql gui tool, phpmyadmin, mysql workbench or whatever you want.

PS: Are you sure you're working on your local machine and your db server is a local one?

Edited by jazzman1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.