|
Posted by J.O. Aho on 07/28/06 17:26
PSI_Orion wrote:
> Is it possible to have functions in include files? I am trying to generate
> a global function include for opening my databases but it comes back and
> says:
>
> Fatal error: Call to undefined function dbaseConnect() in
> D:\Inetpub\wwwroot\psiss.com\dummy\index.php on line 19
>
> The include file is this...
>
> <?
> if(!(include 'XXXXXXXXX'))
> die("<p align='center'><font color='#FF0000'><b>FAILED TO INCLUDE
> 'XXXXXXXXX'</b></font></p>");
>
> function dbaseConnect($dbaseName,$tableName)
> {
> $dbase=@mysql_connect($dbaseName,$dbaseUser,$dbasePW);
> if(!$dbase)
> die("<p align='center'><font color='#FF0000'><b>ERROR CONNECTING TO MySQL
> SERVER</b></font></p>");
> $tables=@mysql_list_tables($tableName,$dbase);
> if(!$tables)
> die("<p align='center'><font color='#FF0000'><b>ERROR RETRIEVING TABLES
> FROM DATABASE '".$dbaseName."'</b></font></p>");
> $FOUND=false;
> for($i = 0; $i < @mysql_num_rows($tables) && (!$FOUND); $i++)
> {
> if(@mysql_tablename($tables,$i) == $tableName)
> $FOUND=true;
> }
> if(!$FOUND)
> die("<p align='center'><font color='#FF0000'><b>ERROR RETRIEVING TABLE
> '".$tableName."'</b></font></p>");
> }
> ?>
>
> and the include stuff in the main file is this which is declared before the
> dbaseConnect() call.
>
> if(!(include 'XXXXXXXXXXX'))
> die("<p align='center'><font color='#FF0000'><b>FAILED TO INCLUDE
> 'XXXXXXX'</b></font></p>");
> dbaseConnect("xxxxxxx","xxxxxxx");
>
> Obviously, for security reasons, I've changed the file names in this code to
> XXXXXXXXXX.
>
--- index.php ---
include_once("database.php");
$tableName="YYYYYY";
if(!dbaseConnect("xxxxxxx",$tableName))
die("<p align='center'><font color='#FF0000'><b>ERROR RETRIEVING TABLE
'".$tableName."'</b></font></p>");
--- eof ---
--- database.php ---
<?PHP
function
dbaseConnect($dbServer,$dbaseName,$tableName,$dbaseUser="user",$dbasePW="pass") {
if(!$link=@mysql_connect($dbServer,$dbaseUser,$dbasePW))
die("<p align='center'><font color='#FF0000'><b>ERROR CONNECTING TO MySQL
SERVER</b></font></p>");
if(!mysql_select_db($databaseName, $link))
die("<p align='center'><font color='#FF0000'><b>ERROR ACCESSING DATABASE
'".$dbaseName."'</b></font></p>");
if(!$tables=@mysql_list_tables($tableName,$link))
die("<p align='center'><font color='#FF0000'><b>ERROR RETRIEVING TABLES
FROM DATABASE '".$dbaseName."'</b></font></p>");
while($name=mysql_fetch_row($tables)) {
if($name[0]==$tableName)
return true;
}
return false;
}
> ?>
--- eof ---
This should work fine.
Navigation:
[Reply to this message]
|