Prepared Statements

------=_NextPart_000_0037_01CBC9FC.8C29EAD0
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable


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 =3D new PDO($dsh, $username, $password);

$stmt =3D $dbh->prepare("SELECT `reference`, `organization`, `city`, =
`province_state`, `postal_zip_code`, `country` FROM `ministry_profiles` =
WHERE ( `reference` =3D :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 =3D $stmt->fetch(PDO::FETCH_ASSOC);

echo "<ul>\r\n";

$search_result_organization_reference =3D $result['reference'];
$search_result_organization =3D $result['organization'];
$search_result_city =3D $result['city'];
$search_result_province_state =3D $result['province_state'];
$search_result_postal_zip_code =3D $result['postal_zip_code'];
$search_result_country =3D $result['country'];

echo "<li><strong>" . $search_result_organization . "</strong> (Ref: =
" . $search_result_organization_reference . ")
\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 Verse of the Day
=E2=80=9CEncouragement from God=E2=80=99s Word=E2=80=9D
http://www.TheVerseOfTheDay.info

------=_NextPart_000_0037_01CBC9FC.8C29EAD0--
ron.piggott [ Fr, 11 Februar 2011 21:01 ] [ ID #2055008 ]

Re: Prepared Statements

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 . ")
\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.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
David Robley [ Sa, 12 Februar 2011 05:50 ] [ ID #2055044 ]
PHP » gmane.comp.php.database » Prepared Statements

Vorheriges Thema: Mysql 5.5 and PHP Mysql API Version 5.1.41
Nächstes Thema: php as cgi and sqlite