Prepared Statements
am 11.02.2011 21:01:23 von ron.piggott
------=_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? =20
Am I correctly retrieving the results, if there is more than 1? =20
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
... I want that area in the loop)
Ron
$dbh =3D new PDO($dsh, $username, $password);=20
$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");=20
$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();=20
$result =3D $stmt->fetch(PDO::FETCH_ASSOC);
echo "\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 "- " . $search_result_organization . " (Ref: =
" . $search_result_organization_reference . ")
\r\n";
echo $search_result_city . ", " . $search_result_province_state . " " . =
$search_result_country . " " . $search_result_postal_zip_code . =
" \r\n";
echo "
\r\n";
The Verse of the Day
â=9CEncouragement from Godâ=99s Wordâ=9D
http://www.TheVerseOfTheDay.info=20
------=_NextPart_000_0037_01CBC9FC.8C29EAD0--
Re: Prepared Statements
am 12.02.2011 05:50:40 von David Robley
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
> ... 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 "\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 "- " . $search_result_organization . " (Ref: "
> . $search_result_organization_reference . ")
\r\n";
> echo $search_result_city . ", " . $search_result_province_state . " " .
> $search_result_country . " " . $search_result_postal_zip_code .
> " \r\n";
>
> echo "
\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