I'm dealing with a method in my DAO which looks up an ID to an entry. In this case, an author. The user-entered data often contains two names as an author entry (old data migrated over not updated yet) like so:
Berenstain, Stan & Jan
The code works flawlessly when looking up something like this:
Aiger-Clark, Julie
But it fails every time there is an & in the author name. Copying and pasting the author into PhpMyAdmin in a search finds the author.
I've verified that $authorName is actually the correct name:
Berenstain, Stan & Jan
I've verified that this fails in both a prepared statement and a normal query. I've also confirmed that it fails with more than one author entry, the common failure point seemingly being the authors containing the & sign. All other tried lookups succeed.
I'm using a PDO connection. It's failing at the row count return point of this method (I have verified that rowCount works with MySQL).
This is the code as it sits:
public function getAuthorIDByName($authorName) {
$dbConn = Connectors::pdoDatabaseConnect();
$sql = "SELECT authorID FROM authors WHERE authorName=?";
if (!$stmt = $dbConn->prepare($sql)) {
return false;
}
if ($stmt->execute(array($authorName)) === false) {
return false;
}
if($stmt->rowCount() != 1) {
return false;
}
$obj = $stmt->fetch(PDO::FETCH_OBJ);
return $obj->authorID;
}
I tried the following, and it failed:
$sql = "SELECT authorID FROM authors WHERE authorName = :authorName";
if (!$stmt = $dbConn->prepare($sql)) {
Log::logEndMethod();
return false;
}
$stmt->bindParam(':authorName', $authorName, PDO::PARAM_STR);
if ($stmt->execute() === false) {
return false;
}