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

#21 jazzman1

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

Posted 26 December 2014 - 03:45 PM

 

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

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


#22 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 03:48 PM

 

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



#23 jazzman1

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

Posted 26 December 2014 - 03:59 PM

Then the user_id column in your database / table is a char type instead integer. Change the type and try again. 



#24 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 04:03 PM

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.



#25 jazzman1

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

Posted 26 December 2014 - 04:14 PM

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


#26 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 04:22 PM

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?

 

 

yes im working on localhost and also my db is on local as i'm using MAMP PRO for mac ,, and btw , i do have also other functions which are working with databse and doing their work successfully and getting the result from database expect this one.

 

will get you the columns structure.


Edited by ArshSingh, 26 December 2014 - 04:26 PM.


#27 jazzman1

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

Posted 26 December 2014 - 04:26 PM

we need to see the result of show columns!



#28 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 26 December 2014 - 04:28 PM

we need to see the result of show columns!

 

please see this screenshot for columns:  http://prntscr.com/5l52ip



#29 jazzman1

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

Posted 26 December 2014 - 07:01 PM

Sorry for the delay I wasn't at home. Are you absolutely sure that you have only one table called - "members" in this local db server? Can I also see your database credentials? There is nothing wrong with the structure in this screenshot. 



#30 kicken

kicken
  • Gurus
  • Wiser? Not exactly.
  • 3,396 posts
  • LocationBonita, FL

Posted 26 December 2014 - 08:33 PM   Best Answer

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.
Did I help you out? Feeling generous? I accept tips via Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7
Kicken's World⦄ ⦃Recycle old CD's

#31 mac_gyver

mac_gyver
  • Staff Alumni
  • Staff Alumni
  • 4,139 posts

Posted 26 December 2014 - 08:35 PM

the symptom you are seeing is likely due to a page being requested twice, once with and once without the expected input data. and given that you are doing url rewriting, with your locahost/easy/u/5, this is even more likely.

 

if you look in your web server's access log, i'm betting you have two requests for your page, one with and one without the correct get query string on it. you may even have a get query string that has added a space character as part of the value. if so, it's possibly due to a faulty url rewriting rule, or your page redirecting to itself a second time, or the browser requesting the page twice.

 

however, ALL code should ALWAYS validate data before trying to use it. you need to make sure there's an expected value for the user id before ever calling the class method. making sure that the value contains all numerical characters (see ctype_digit()) and is not an empty string will at least verify that it contains a likely user id.

 

also, why are you using a default $user_id = null call time parameter for a function that requires a value for that parameter to work (defining default parameter values are intended for optional parameters, where the function will still perform its intended purpose when the parameter is not present), assigning one variable to another ($user = $user_id;), and then looping over a result set that by definition will at most be one row? when we see code that contains extra lines and statements that don't accomplish anything useful, we wonder what you were thinking when you typed those things into your code? code should just contain those things that are necessary and that contribute to the goal that code is trying to accomplish.


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting.

#32 ArshSingh

ArshSingh
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 04 January 2015 - 06:01 PM

there was nuthing to do rewrite things ,  noob me was using while loop :( . thnx to kicken now all is working good and smoothly :).

kicken 's answer worked 




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users