0

I have a list of users ids and my goal is to get the name of each user using the id

the sql variable prints: SELECT name FROM users WHERE unique_id = '56d4814fb37cf3.17691034 '

If i copy paste the query the name is returned and works as well with other ids, but i don't want the function to allways return the same name, i want to add the variable $userID to my query and return the name

But this only works when i hardcode de id

public function returnNameByID($userID){
 //User ID: 56d4814fb37cf3.17691034 
	
    $sql =  "SELECT name FROM users WHERE unique_id = '$userID'";
	
  echo $sql; // Prints SELECT name FROM users WHERE unique_id = '56d4814fb37cf3.17691034 '
	
//Doesn't work $name returns Null
//  $stmt = $this->conn->prepare("SELECT name FROM users WHERE unique_id = '$userID'"); 

//Doesn't work $name returns Null
	$stmt = $this->conn->prepare($sql); 

// Works $name returns name of the user
   $stmt = $this->conn->prepare(" SELECT name FROM users WHERE unique_id = '56d4814fb37cf3.17691034 ' "); 

    $stmt->execute();
	
    $result = $stmt->get_result()->fetch_assoc();
	
    $name = $result["name"];

    return $name;

EDIT

require_once 'db_functons.php';
$db = new db_functions();

$userID = $_GET['userID'];
$mArray =  array();

$mArray = $db->getFriendsList($userID);
//Printing the array Prints the correct id's

$name = $db->returnNameByID($mArray[0]);
echo $name;

9
  • I removed the extraneous database tags. Feel free to add the tag for the database you are really using. Commented Mar 13, 2016 at 19:02
  • 2
    '56d4814fb37cf3.17691034 ' see that trailing space? Remove it. Commented Mar 13, 2016 at 19:02
  • this line works: $stmt = $this->conn->prepare(" SELECT name FROM users WHERE unique_id = '56d4814fb37cf3.17691034 ' "); removing the space makes no diference, but this // $stmt = $this->conn->prepare("SELECT name FROM users WHERE unique_id = '$userID'"); and this $stmt = $this->conn->prepare($sql); don't Commented Mar 13, 2016 at 19:07
  • 1
    i used echo gettype ($mArray[0]); prints string Commented Mar 13, 2016 at 19:27
  • 1
    The problem is that the $userID in the function has some random blank characters on the end of it. On the first line of your function $userID = trim($userID); Commented Mar 13, 2016 at 19:27

1 Answer 1

2

Seems you have some space around your code try using a trim

$sql =  "SELECT name FROM users WHERE trim(unique_id) = '" . trim($userID) . "';";
Sign up to request clarification or add additional context in comments.

2 Comments

Yay, someone gets it. One thing that I would suggest is just adding that you should probably bind the parameter $userID rather than inserting it directly into the query (just good practice)
Yes in some case the string construction is not clear and in the way i proposed the evaluation of the var content is explicitally clear ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.