Re: PEAR::DB and MySQL enum
| From: | Tomas V.V.Cox | Date: | Fri, 15 Mar 2002 00:15:37 +0000 |
| Subject: | Re: PEAR::DB and MySQL enum | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-964@lists.php.net to get a copy of this message | ||
"Yoav M. Felberbaum" wrote:
>
> Hello,
>
> At the moment, I have this in my code (pre-PEAR):-
> -------------
> <?php
>
> while ($answer = mysql_fetch_array($result))
> {
> ereg( "('(.*)')", $answer[1], $temp
> );
> $array = explode( "','", $temp[2]
> );
>
> while (list ($key, $val) = each ($array))
> {
> echo "<option
> value=\"$val\">$val</option>\n";
> }
> }
> echo "</SELECT>";
> ?>
> -------------
> Basically, what this does is to create an option list/pop-down list from an enum field in a
> MySQL database.
> But, the problem is I can not seem work out how this is done "via" the PEAR
> interface?
> Is this even possible?
>
Just replace:
$result = mysql_query($sql);
while ($answer = mysql_fetch_array($result))
with:
$res = $db->query($sql);
while ($answer = $res->fetchRow())
Of course you could complicate more your self with PEAR DB and use the
"row object" feature. Something like:
<?php
class MyOption extends DB_Row {
function MyRow(&$row) {
preg_match_all("/'(.*)'/U", $row[1], $m);
foreach($m[1] as $val) {
echo "<option value=\"$val\">$val</option>\n";
}
}
}
$db->setFetchMode(DB_FETCHMODE_OBJECT, 'MyOption');
while ($res->fetchRow());
echo '<SELECT/>';
?>
Sorry for this second example, was just to illustrate some hidden
features of PEAR DB :-)
Tomas V.V.Cox