Re: MySql Statement inside a function?

From: Date: Sat, 20 Oct 2001 10:22:26 +0000
Subject: Re: MySql Statement inside a function?
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-22557@lists.php.net to get a copy of this message
I think you have forgotten that the variable $tbn2 is not available in your function, unless you include it into your function, using the "global" statement. Try this code: function getnames(){ global $tbn2; $gnlistsql = "select name from $tbn2"; $gnlistres = mysql_query($gnlistsql); while($nlist = mysql_fetch_array($gnlistres)){ $nlistop = "<option value=\"$nlist[name]\">$nlist[name]</option>"; $nlistsel = "<select name=\"getname\">$nlistop</select>"; echo $nlistsel; } } "Dave Carrera" <dave@davecarrera.com> wrote in message news:000001c276be$4d93a260$759e27d9@phpdevbox001... > Hi All > > I have created a function and insde that function it dose a standard > select, fetch_arry, while loop. > > I get a error from mysql saying not a vaild resource but if I take the > statemnet out of the function all works fine and dandy. > > Are there things I should be aware of when trying to include mysql > statements inside a function? > > I have include my code here if it helps. > > function getnames(){ > $gnlistsql = "select name from $tbn2"; > $gnlistres = mysql_query($gnlistsql); > while($nlist = mysql_fetch_array($gnlistres)){ > $nlistop = "<option > value=\"$nlist[name]\">$nlist[name]</option>"; > $nlistsel = "<select name=\"getname\">$nlistop</select>"; > echo $nlistsel; > } > } > > There error is reported back in the while line... > > Any help or advice is appreciated as always. > > P.S reason for the fuction is that I will call the select a name list a > few times so I thought of making it a function then calling it when > needed. > > Thank You > > Dave C >

« previous php.db (#22557) next »