I am experiencing an issue with the values being returned from my PDO statement.
This is my code:
//Execute test
$this->checkConnect();
$stmt = $this->dbh->prepare("SELECT p_id FROM People WHERE lastName = :param1 AND firstName = :param2");
$stmt->bindParam(':param1', $this->lName);
$stmt->bindParam(':param2', $this->fName);
$stmt->execute();
$count = $stmt->rowCount();
//Determine value of test
if($count == FALSE)
{
return FALSE;
}
else
{
$dummyvar = $stmt->fetch();
$this->p_id = implode($dummyvar);
}
When I was going through my database records, I noticed that a certain value was off from what I had input. When I execute a query, it is supposed to grab the value of p_id from the tablePeople. Simple enough. However, what happens is that the number is appended twice to itself. For instance, say p_id is equal to 1. this->p_id will be equal to 11. Or is p_id is equal to 2, the output will be 22. I’ve executed this query within MySQL and the value is correct. I’m not sure what is happening in my php code. Perhaps something to do with implode? I’m not sure.
Any insight will be appreciated.
Addition: I should also state that p_id is unique, thus only one value can be returned.
First, your fetch statement isn’t returning what you think it is. The default output array will have both column name keys, and numeric keys, something like this:
You probably want to get just a numerically-indexed array. Use
PDO::FETCH_NUMlike this:Second, if you are going to output more than one field (not in this case obviously) then you have to fix your implode statement. You have to tell it what character to put between the different array values.
For example:
References: