ArshSingh Posted December 26, 2014 Share Posted December 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/ Share on other sites More sharing options...
Jacques1 Posted December 26, 2014 Share Posted December 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500679 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 (edited) 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 December 26, 2014 by ArshSingh Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500680 Share on other sites More sharing options...
ginerjm Posted December 26, 2014 Share Posted December 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500682 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500687 Share on other sites More sharing options...
ginerjm Posted December 26, 2014 Share Posted December 26, 2014 Did you notice the key difference in your two statements? And - you didn't answer my questions. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500689 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 (edited) 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 December 26, 2014 by ArshSingh Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500690 Share on other sites More sharing options...
ginerjm Posted December 26, 2014 Share Posted December 26, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500691 Share on other sites More sharing options...
jazzman1 Posted December 26, 2014 Share Posted December 26, 2014 i have also done _ var_dump of $user before query Can you show us that result? Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500693 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 Can you show us that result? Well result of var_dump is : string 0 '5' Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500696 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500698 Share on other sites More sharing options...
ginerjm Posted December 26, 2014 Share Posted December 26, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500700 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 (edited) 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 December 26, 2014 by ArshSingh Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500701 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 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" Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500702 Share on other sites More sharing options...
jazzman1 Posted December 26, 2014 Share Posted December 26, 2014 (edited) 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 December 26, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500704 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 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" Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500705 Share on other sites More sharing options...
jazzman1 Posted December 26, 2014 Share Posted December 26, 2014 Now, it should work properly. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500706 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 (edited) 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 December 26, 2014 by ArshSingh Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500707 Share on other sites More sharing options...
jazzman1 Posted December 26, 2014 Share Posted December 26, 2014 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(). Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500709 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 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" Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500710 Share on other sites More sharing options...
jazzman1 Posted December 26, 2014 Share Posted December 26, 2014 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 ............................... } Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500711 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500712 Share on other sites More sharing options...
jazzman1 Posted December 26, 2014 Share Posted December 26, 2014 Then the user_id column in your database / table is a char type instead integer. Change the type and try again. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500714 Share on other sites More sharing options...
ArshSingh Posted December 26, 2014 Author Share Posted December 26, 2014 Then the user_id column in your database / table is a char type instead integer. Change the type and try again. it's already integer. Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500715 Share on other sites More sharing options...
jazzman1 Posted December 26, 2014 Share Posted December 26, 2014 (edited) 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 December 26, 2014 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/293365-php-pdo-variable-not-working-in-query-but-static-value-works/#findComment-1500718 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.