Re: Prepared Statements

From: Date: Sat, 12 Feb 2011 04:50:40 +0000
Subject: Re: Prepared Statements
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-47677@lists.php.net to get a copy of this message
Ron Piggott wrote: > > I am just starting to use Prepared Statements and am in a learning curve. > > I am working on the code below. It is for a directory. This is to select > the listing for updating. > > How do I tell if there are more than 1 search result? > > Am I correctly retrieving the results, if there is more than 1? > > I want to make a WHILE loop to display the search results for the listing > the user is editing. (You can see where I have started the <ul> ... </ul> > ... I want that area in the loop) > > Ron > > > $dbh = new PDO($dsh, $username, $password); > > $stmt = $dbh->prepare("SELECT reference, > organization, city, > province_state, > postal_zip_code, country FROM > ministry_profiles > WHERE ( reference = :organization_reference ) OR ( > organization LIKE > %:organization_name% ) OR ( telephone LIKE > %:organization_phone% ) OR ( > toll_free LIKE %:organization_toll_free_phone% ) ORDER BY > organization > ASC"); > > $stmt->bindParam(':organization_reference', $organization_reference, > PDO::PARAM_STR); $stmt->bindParam(':organization_name', > $organization_name, PDO::PARAM_STR); > $stmt->bindParam(':organization_phone', $organization_phone, > PDO::PARAM_STR); $stmt->bindParam(':organization_toll_free_phone', > $organization_phone, PDO::PARAM_STR); > > $stmt->execute(); > > $result = $stmt->fetch(PDO::FETCH_ASSOC); > > echo "<ul>\r\n"; > > $search_result_organization_reference = $result['reference']; > $search_result_organization = $result['organization']; > $search_result_city = $result['city']; > $search_result_province_state = $result['province_state']; > $search_result_postal_zip_code = $result['postal_zip_code']; > $search_result_country = $result['country']; > > echo "<li><strong>" . $search_result_organization . > "</strong> (Ref: " > . $search_result_organization_reference . ")<br />\r\n"; > echo $search_result_city . ", " . $search_result_province_state . " " . > $search_result_country . " " . $search_result_postal_zip_code . > "</li>\r\n"; > > echo "</ul>\r\n"; The docs are a good source of information. For instance, example 2 on http://php.net/manual/en/pdostatement.fetch.php looks to be one solution. Or http://php.net/manual/en/pdostatement.fetchall.php which gives you an array of all the result set rows. Cheers -- David Robley To shoot a mime, do you use a silencer? Today is Pungenday, the 43rd day of Chaos in the YOLD 3177.

« previous php.db (#47677) next »