Re: Converting code to a function

From: Date: Wed, 28 Feb 2001 03:16:10 +0000
Subject: Re: Converting code to a function
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-41948@lists.php.net to get a copy of this message
Thanks :-) ----- Original Message ----- From: "Simon Garner" <sgarner@expio.co.nz> To: "Clayton Dukes" <cdukes@cdukes.com>; <php-general@lists.php.net> Sent: Tuesday, February 27, 2001 9:59 PM Subject: Re: [PHP] Converting code to a function > Clayton, > > The problem is that some of your variables, e.g. $HOSTNAME, $DB_USER, etc., > are not global variables, and so are not accessible from inside the > function. > > I recommend making those settings define()s instead of variables, but if you > keep them as variables then you just need to add: > > > <?php > function viewdoc ($docid) > { > global $HOSTNAME, $DB_USER, ...; > ... > } > ?> > > > But to do them as constants instead, do: > > <?php > define("HOSTNAME", "myhost.net"); > define("DB_USER", "myuser"); > define("DB_PASS", "jksajfeioe"); > > function viewdoc ($docid) > { > ... > $dbh = mysql_connect(HOSTNAME, DB_USER, DB_PASS); > ... > } > ?> > > > Hope this helps. > > > Cheers > > Simon Garner > > > ----- Original Message ----- > From: "Clayton Dukes" <cdukes@cdukes.com> > To: <php-general@lists.php.net> > Sent: Wednesday, February 28, 2001 3:51 PM > Subject: [PHP] Converting code to a function > > > Hi everyone :-) > > I need help, how can I convert the following code into a function so that I > can just place it in a global include file and call it using > viewdoc($docid); whenever necessary? > > -------START------- > if ($docid == "") { > echo "Fatal Error - DocID not supplied"; > exit; > } > > $dbh = mysql_connect("$HOSTNAME","$DB_USER","$DB_PASS"); > mysql_select_db("$DATABASE"); > > $sth = mysql_query("SELECT docdata FROM documents WHERE docid like > '$docid'"); > > $row = mysql_fetch_array($sth); > > if ($row[0] != "") { > echo "DocID: $docid\n\n"; > echo (wraptext($row[0],72)); > > } else { > echo "Fatal Error - Can't fetch docid"; > } > -------END------- > > I tried to do this: > function viewdoc($docid) { > ...PASTE FROM ABOVE... > } > > But then I get: > > Warning: Supplied argument is not a valid MySQL result resource in > /home/www/websites/gdd/inc/funcs.inc on line 76 > Fatal Error - Can't fetch docid > > when I call it from a web page using: > > viewdoc($docid); > > > What have I done wrong? > > > > TIA! > Clayton Dukes > > > > > -------------------------------------------------------------------------- -- > ---- > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > > For additional commands, e-mail: php-general-help@lists.php.net > > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#41948) next »