note 60408 deleted from function.mysqli-prepare by mazzanet

From: Date: Fri, 06 Jan 2006 10:05:02 +0000
Subject: note 60408 deleted from function.mysqli-prepare by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-101296@lists.php.net to get a copy of this message
Note Submitter: wostner at cyberprof dot com ---- Here is an example using bind_param and bind_result, and iteration over a list of cities. Note that there's some bug-potential in cases where the query returns NULL for some parameter value, but the bind_result variables still might be bound. So, we use a conditional to spray the spot first. $mysqli->select_db("world"); $template = "SELECT District, CountryCode FROM City WHERE Name=?"; printf("Prepare statement from template: %s\n", $template); $cities = array('San Francisco', 'Lisbon', 'Lisboa', 'Marrakech', 'Madrid'); printf("Cities: %s\n", join(':', $cities)); if ($stmt = $mysqli->prepare($template)) { foreach($cities as $city) { // bind the string $city to the '?' $stmt->bind_param("s", $city); $stmt->execute(); // bind result variables $stmt->bind_result($d,$cc); // 'Lisbon' is not found in the world.City table, but 'Lisboa' is. // Using a conditional we avoid putting Lisbon in California. if($stmt->fetch()) { printf("%s is in %s, %s\n", $city, $d, $cc); } } $stmt->close(); } With the conditional statement we get the desired result: Prepare statement from template: SELECT District,CountryCode FROM City WHERE Name=? Cities: San Francisco:Lisbon:Lisboa:Marrakech:Madrid San Francisco is in California, USA Lisboa is in Lisboa, PRT Marrakech is in Marrakech-Tensift-Al, MAR Madrid is in Madrid, ESP But, without the conditional statement we would put Lisbon in California. San Francisco is in California, USA Lisbon is in California, USA Lisboa is in Lisboa, PRT Marrakech is in Marrakech-Tensift-Al, MAR Madrid is in Madrid, ESP

« previous php.notes (#101296) next »