php - mysqli query returning wrong values -
I am creating a function that returns the requested fields of some users connected to a user ID, my current code is :
& lt ;? Php function getUserData ($ id = "current", $ field = "user name") {global $ database; If ($ id == "current") {$ id = $ _SESSION ['userID']; } $ Query = $ Database-> Ready ("Select: Field from User" where `id` =: Id range 1;); $ Query- & gt; bindParam (": id ", $ id); $ Query- & gt; bindParam (": Area $ (Field); $ Query- & gt; executed; while ($ line = $ query- & gt; receive (PDO :: FETCH_BOTH)) {print_r ($ line);} // return $ username ;}? & Gt;
If the $ id
value is empty then it currently appears for the login ID, which works fine If this is empty, then for this code from $ field
The only user name that appears is the problem that is in the $ query-> bindParam ("field", $ field);
.. for some reason this does not work and when using it print_r It gives:
Array ([username] = & gt; username [0] = & gt; username)
While in this way The exact same query works when using it:
$ query = $ Database-> Ready ("SELECT $ field FROM
user WHERE
Id =: ID range 1; ");
What am I doing?
The name of the field is bound, so your query will be created like this:
SELECT 'username' FROM `user` where` id` = 'x' range1;
< / Pre>This will not work, you can not bind a field name like this. You must pass field names as no compulsive field names.
See this:
This can help you.
Comments
Post a Comment