Mysqli::prepare + select ... where ... in + array

From: Date: Tue, 26 Aug 2008 08:35:15 +0000
Subject: Mysqli::prepare + select ... where ... in + array
Groups: php.general 
Request: Send a blank email to php-general+get-278712@lists.php.net to get a copy of this message
Hey there I have been toying around with the following code for a few days: <?php //Gets the tokens defined in the array $tokensToGet, with the language defined by $languageId public static function GetTokens($tokensToGet, $languageId) { $query = @"SELECT Name, Value FROM InterfaceLanguageTokens WHERE Name IN (?) AND InterfaceLanguageId = ?"; $tokens = array(); $mysqli = Database::Connect(); if($stmt = $mysqli->prepare($query)) { $stmt->bind_param('si', $tokensToGet, $language); $stmt->Execute(); $stmt->Bind_result($name, $value); while($stmt->fetch()) { $tokens [] = array("name" => $name, "value" => $value); } $stmt->Close(); } $mysqli->close(); return $tokens; } ?> The problem is this: WHERE NAME IN (?) – I would consider it logical that the prepare statement can take a type of array – in this case an array of strings that I then with the bind_param statement, with the type "s". Unfortunately this doesn't work. To get around this problem I have to go around the problem and use string gymnastics – something like this: $tokensToGetString = "'" . implode("','", $tokensToGet) . "'" and add that instead of the (?). I hope you can help me out in this matter. Thank you in advance. Hans Henrik Petersen System Developer

« previous php.general (#278712) next »