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" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]