I’ve used SELECT many times and I can’t figure out why this one function is failing to find data that is there. Please note in the code examples that the original intent of the functions is temporarily put aside for the purposes of debugging this problem.
The database table is called “monsterlist”. The table contains many columns, the first two being “ID” and “Name”.
The function I have created is short and simple:
function MonsterGetIDfromName($name)
{
global $db;
$dq = $db->prepare("SELECT ID FROM monsterlist WHERE Name =".$name);
$dq->execute();
$ID=$dq->fetch();
if($ID)
{
return "found something";
}
$ID=$ID["ID"];
$dq->closeCursor();
if($ID)
{
return $ID;
}
else
{
return $name;
}
}
The test code in the page is:
$encounter_info = MonsterGetIDfromName(“Goblin”);
and somewhat later:
echo (“window.alert(’”.$encounter_info."’);\n");
Running the page results in a window.alert with “Goblin” shown, which indicates that function failed to set $ID equal to anything.
I have verified via phpmyadmin that for ID=156, Name=Goblin.
EDIT: Sorry for the sloppy-looking code, it says entering 4 spaces before a line makes it auto-format as code but that doesn’t seem to be working.