Re: How can I get just one row from selected column?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: How can I get just one row from selected column?

From: Gazelem <usenet@enhanced.org>
Date: Wed Jan 04 2006 - 00:27:09 CET

Leszek wrote:
> Hi.
> How can I get just one row from selected column and put it into html
> dropdown list
> I tried like this:
>
> function pobierz_wszystko($tabela,$kolumna)
> {
> $zapytanie="SELECT $kolumna FROM $tabela";
> $wynik=mysql_query($zapytanie);
> while($wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC))
> {
> echo "<option value=$wiersz>$wiersz</option> <br />";
> }
> }

The query is wrong among other things.

proper: Select $kolumna from $tabela limit 1

You can also use an offset, reference the manual at
http://dev.mysql.com/doc/refman/4.1/en/select.html

Another thing is, if you are *always* going to want just one result, not
only should you use the proper select, but you should also limit your code
to only ask for 1 result:

$wynik=mysql_query($zapytanie);
$wiersz=mysql_fetch_array($wynik,MYSQL_ASSOC)
echo "<option value=$wiersz>$wiersz</option> <br />";

notice the lack of using a "while" statement, which is not appropriate for 1
result queries.

-Dave
Received on Tue Jan 17 16:52:09 2006