Jump to content


Photo

PHP PDO variable not working in query but static value works

php pdo variables

Best Answer kicken, 26 December 2014 - 08:33 PM

public function profile_view($user_id = null) {
	$stmt = $this->_db->prepare('SELECT memberID,username,email,profile_pic,active FROM members WHERE memberID = :user_id AND active="YES"');

	$params = array(':user_id' => $user_id);
	var_dump($params);
	$stmt->execute($params);

	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	var_dump($row);
	if ($row){
		$user_det = (object)array(
			'username'=> $row['username']
			,'email'=>$row['email']
			,'profile_pic'=>$row['profile_pic']
			,'id'=> $row['memberID']
			,'active'=>$row['active']
		);

		return $user_det;
	}
	else {
		var_dump($stmt->errorInfo());
	}
}

Since you only want one row, there is no need for the loop. Check the return value of the fetch call to be sure a row was actually found. I've added a few var_dumps at places that might be helpful in resolving your problem. Go to the full post


  • Please log in to reply
31 replies to this topic

#1 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 11:18 AM

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.


#2 Jacques1

Jacques1
  • Members
  • PipPipPip
  • Turtles all the way down
  • 4,224 posts

Posted 26 December 2014 - 11:25 AM

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.



#3 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 11:29 AM

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, 26 December 2014 - 11:30 AM.


#4 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,083 posts
  • LocationVoorheesville NY

Posted 26 December 2014 - 12:16 PM

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.


JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#5 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 12:53 PM

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.



#6 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,083 posts
  • LocationVoorheesville NY

Posted 26 December 2014 - 12:55 PM

Did you notice the key difference in your two statements?

And - you didn't answer my questions.


JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#7 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 01:00 PM

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, 26 December 2014 - 01:01 PM.


#8 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,083 posts
  • LocationVoorheesville NY

Posted 26 December 2014 - 01:11 PM

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?


JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#9 jazzman1

jazzman1
  • Moderators
  • Advanced Member
  • 2,715 posts
  • LocationMississauga, Canada

Posted 26 December 2014 - 01:14 PM

 

i have also done _ var_dump of $user before query

Can you show us that result?



#10 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 02:29 PM

Can you show us that result?

 Well result of var_dump is : string 0 '5'



#11 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 02:34 PM

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.



#12 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,083 posts
  • LocationVoorheesville NY

Posted 26 December 2014 - 02:40 PM

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.


JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#13 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 02:49 PM

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, 26 December 2014 - 02:50 PM.


#14 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 02:54 PM

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"

#15 jazzman1

jazzman1
  • Moderators
  • Advanced Member
  • 2,715 posts
  • LocationMississauga, Canada

Posted 26 December 2014 - 03:14 PM

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, 26 December 2014 - 03:21 PM.


#16 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 03:21 PM

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"


#17 jazzman1

jazzman1
  • Moderators
  • Advanced Member
  • 2,715 posts
  • LocationMississauga, Canada

Posted 26 December 2014 - 03:23 PM

Now, it should work properly.



#18 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 03:25 PM

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, 26 December 2014 - 03:27 PM.


#19 jazzman1

jazzman1
  • Moderators
  • Advanced Member
  • 2,715 posts
  • LocationMississauga, Canada

Posted 26 December 2014 - 03:33 PM

 

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(). 



#20 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 03:41 PM

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"





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users