You are here: Re: Confused by mysqli « PHP Programming Language « IT news, forums, messages
Re: Confused by mysqli

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация