I'd agree with Rob especially about returning the results.

Plus it's always a good idea to add error checking to your SQL statement.
Try replacing your existing line of code with this to make sure you query is
actually returning valid results before trying to operate on them.

$gnlistres = mysql_query($gnlistsql)
        or die(mysql_error());

(I think that syntax is right).

I also don't see where you're actually setting up the db connection and
selecting a database. Are you doing so within the scope of this script?

Finally, within the while loop, I've always used statements like $var =
$row["colName"]. I noticed you don't have yours quoted. Don't know if that
makes a difference. The error you're getting is before those lines anyway,
so maybe not.

Just my $.03. Inflation.

Hope this helps.

> -----Original Message-----
> From: Dave Carrera [mailto:dave@;davecarrera.com]
> Sent: Friday, October 18, 2002 11:52 AM
> Subject: [PHP-DB] MySql Statement inside a function?
> 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
> -- 
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to