You need to call it such as:
$variable = query("field","table");
Where $variable is an arbitrary name and is what you'd later use in the
mysql_fetch_*() functions...
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/
> -----Original Message-----
> From: scott [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, April 02, 2003 7:06 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] trying to write sql query as a function
>
> hi
>
> I'm trying to write a simple MySQL query in a php function so that I
can
> just call it and pass arguments, but I'm missing something :o(
>
> there are 2 files, index.php and common.php, commom.php contains all
the
> functions, and is included at the beginning of index.php
>
> the first function called in index.php is db_connect, which calls the
> function to create a connect to the MySQL database, this would seem to
be
> working correctly
>
> the second part (here's where I crap out) is the actual query. the
> function
> is written as follows
>
> function query($strField, $strTable) {
> $sql = "select $strField from strTable";
> $result = mysql_query($sql);
> return $result;
> }
>
> I call it with query(my_field, my_table) for example, and I would hope
it
> would return $result back to the calling page for further us, but the
rest
> of index.php
>
> print "<SELECT name=item>";
> while ($line = mysql_fetch_row($result))
> {
> foreach ($line as $value)
> {
> print "<OPTION value='$value'";
> }
> print ">$value</OPTION>";
> }
> mysql_close($my_conn);
> print "</SELECT>";
>
> simply renders an empty list box :o( if I take the code out of a
function
> and place it in index.php like this
>
> $sql = "select my_field from my_table";
> $result = mysql_query($sql);
> print "<SELECT name=item>";
> while ($line = mysql_fetch_row($result))
> {
> foreach ($line as $value)
> {
> print "<OPTION value='$value'";
> }
> print ">$value</OPTION>";
> }
> mysql_close($my_conn);
> print "</SELECT>";
>
> then it works fine ?? what am I missing here ?
>
> thanks
>
> _scott
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php