|
Posted by Sandman on 07/05/06 10:41
In article <1152094733.815375.105700@a14g2000cwb.googlegroups.com>,
"Dodger" <el.dodgero@gmail.com> wrote:
> $dbh = new mysqli('localhost','user','password','database');
> if ($err = mysqli_connect_errno()) {
> print "Connect failed: $err";
> exit();
> }
>
> $query = <<<EOF
> SELECT *
> FROM cities
> WHERE Country = ?
> EOF;
>
> $statement = $dbh->prepare($query);
> $statement->bind_param('USA');
> $statement->execute();
> $statement->store_result();
>
> $cities = array();
> $i = 0;
> while ($city = $statement->fetch_assoc()) {
> $cities[$i] = $city;
> $i++;
> }
>
> foreach ($cities as $k => $v) {
> # do stuff over each city... keys of hashref are same as
> # column names in table
> }
>
> But it doesn't work. I get that messed up error string that says the
> fetch_assoc() method doesn't exist...
I've not used "mysqli", but doing the above in mysql:
<?
$db = mysql_connect("host", "user", "password");
$q = mysql_query("select * from cities where country = 'USA'");
while ($r = mysql_fetch_assoc()){
$cities[] = $r["city"];
}
foreach ($cities as $city){
# Do stuff
}
?>
Why the extra foreach? Just "do stuff" in the mysql while loop.
--
Sandman[.net]
Navigation:
[Reply to this message]
|