tready29483 Posted January 28, 2007 Share Posted January 28, 2007 Ok,I have a php page full of functions and I want to include it to another page and call the functions. But for some reason I can't figure out how to do so. I'm self taught php so I'm not that great at figuring out the problem. Here is an example:<?php# Functions Page/***************************************** * Function: Connect To MySQL*****************************************/function mysqlconnect() { # Database Connection Variables $dbhost = 'localhost'; $dbname = 'dbname'; $dbuser = 'username'; $dbpword = 'pword'; # Connection mysql_connect($dbhost, $dbuser, $dbpword) or die(mysql_error()); # Select Database mysql_select_db($dbname) or die(mysql_error()); }/***************************************** * End Connect To MySQL Function*****************************************/?><?php# Index.php# Include page with functionsinclude('functions.php');# Call Db connectionsmysqlconnect();?>Fatal error: Call to undefined function: mysqlconnect() on LineCan anyone see what i am doing wrong or give me an example of a working linked function page?? Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/ Share on other sites More sharing options...
Daney11 Posted January 28, 2007 Share Posted January 28, 2007 Hi mate,Use this...[code]<?phpfunction ConnectDatabase(){global $db;$db = mysql_connect("hostname","username","password") or die("Unable To Connect To The MySQL Database");mysql_select_db("databasename", $db) or die("Unable To Select The Database");}?>[/code]Then on pages use.[code]<?phpinclude_once('functions.php'); // This Connects To The MySQL DatabaseConnectDatabase();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-171075 Share on other sites More sharing options...
trq Posted January 28, 2007 Share Posted January 28, 2007 There is nothing wrong with your code, and no, you do not need to declare $db as a global. Are you sure functions.php is in the same directory as index.php? Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-171077 Share on other sites More sharing options...
tready29483 Posted January 29, 2007 Author Share Posted January 29, 2007 Hey,yes, it is in the same directory. Because it finds one of the other functions I have in functions.php. I thought I typed everything correctly. I just can't figure it out. I guess I need to post the whole files that way you guys can see every detail and nitpick my self taught coding skills. I'm an ASP developer by trade so php is very new to me.<?php/************************************************************** * Programmer: Tommy Ready * Purpose: Index * Language: PHP * File Name: index.php * URL: * Description: This is the home page for the demo site.**************************************************************/session_start();//include("http://www.smoothestcreations.com/dev/demo/includes/variables.php");# Client Variables$_SESSION['ClientID'] = "1";$_SESSION['Contact'] = "Company Owner";$_SESSION['Phone'] = "843.555.5555";$_SESSION['Email'] = "demo@democompany.com";$_SESSION['Theme'] = "theme1"; $functions = 'http://www.smoothestcreations.com/dev/demo/themes/'. $_SESSION['Theme'] .'/includes/functions.php';require $functions; mysqlconnect();$clientID = $_SESSION['ClientID'];$slogan = $_SESSION['Slogan'];$sitetitle = $_SESSION['sitetitle'];clientInfo($clientID);$css = 'http://www.smoothestcreations.com/dev/demo/themes/'. $_SESSION['Theme'] .'/css/style.css';pgheader($css, $sitetitle, $slogan, $clientID);?><?php/************************************************************** * Programmer: Tommy Ready * Purpose: Manage all functions * Language: PHP * File Name: functions.php * URL: * Description: This is the page the holds all functions. * DO NOT EDIT THIS PAGE**************************************************************//***************************************** * Function: Connect To MySQL*****************************************/function mysqlconnect() { # Database Connection Variables $dbhost = 'localhost'; $dbname = 'demo'; $dbuser = 'demo'; $dbpword = 'demo'; # Connection mysql_connect($dbhost, $dbuser, $dbpword) or die(mysql_error()); # Select Database mysql_select_db($dbname) or die(mysql_error()); }/***************************************** * End Connect To MySQL Function*****************************************//***************************************** * Function: Build Main Menu*****************************************/function mainmenu($clientID) { global $clientID; # Build Main Menu Query $MMenuQuery = 'SELECT PG_Name, PG_Title FROM `client_pages` WHERE ClientID="'. $clientID .'" AND PG_Status=1;'; # Run Query $MMenuResults = mysql_query($MMenuQuery); # Display Main Menu $MMnumber = mysql_numrows($MMenuResults); // Count the results if ($MMnumber == 0) { // If There are no results, display generic menu echo '<div class="bar">'. "\n"; echo '<ul>'. "\n"; echo '<li class="slogan">Menu:</li>'. "\n"; echo '</ul>'. "\n"; echo '</div>'. "\n"; } else { // If there are results, create menu echo '<div class="bar">'. "\n"; echo '<ul>'. "\n"; echo '<li class="slogan">Menu:</li>'. "\n"; while ($MMrow = mysql_fetch_array($MMenuResults)) { echo "<li><a href='?page=". $MMrow["PG_Name"] ."'>". $MMrow["PG_Title"] ."</a></li>". "\n"; } echo '</ul>'. "\n"; echo '</div>'. "\n"; }}/***************************************** * End Build Main Menu Function*****************************************//***************************************** * Function: Display Subtitle*****************************************/function subtitle($clientID) { global $clientID; //echo $clientID; # Define Page Value if (empty($_GET["page"])) { $page = "home"; } else { $page = $_GET["page"]; } # Build Subtitle Query $subquery = 'SELECT PG_Subtitle FROM `client_pages` WHERE ClientID="'. $clientID .'" AND PG_Name="'. $page .'";'; # Run Query $subresults = mysql_query($subquery) or die(mysql_error()); # Display Subtitle $subnumber = mysql_numrows($subresults); if ($subnumber != 0) { echo '<div class="subheader">'. "\n"; echo '<p>'. "\n"; while ($subrow = mysql_fetch_array($subresults)) { echo $subrow["PG_Subtitle"]; } echo '</p>'. "\n"; echo '</div>'. "\n"; }}/***************************************** * End Display Subtitle Function*****************************************//***************************************** * Function: Build Page Header*****************************************/function pgheader($css, $sitetitle, $slogan, $clientID) { global $clientID, $css, $sitetitle, $slogan;echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'. "\n";echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'. "\n";echo '<head>'. "\n";echo '<meta http-equiv="content-type" content="text/html;charset=iso-8859-2" />'. "\n";echo '<link rel="stylesheet" href="'. $css .'" type="text/css" />'. "\n";echo '<title>'. $sitetitle .'</title>'. "\n";echo '</head>'. "\n";echo '<body>'. "\n";echo '<div class="content">'. "\n";echo '<div class="header_right">'. "\n";echo '<div class="top_info">'. "\n";echo '<div class="top_info_right">'. "\n";echo '<p><b>You are not Logged in!</b> <a href="#">Log in</a> or <a href="#">register</a> to start downloading music!</p>'. "\n"; echo '</div>'. "\n"; echo '</div>'. "\n";//echo $clientID . "<br />";mainmenu($clientID);echo '</div>'. "\n";echo '<div class="logo">'. "\n";echo '<h1><a href="#">'. $sitetitle .'</a></h1>'. "\n";echo '<p>'. $slogan .'</p>'. "\n";echo '</div>'. "\n";echo '<div class="search_field">'. "\n";echo '<form method="post" action="">'. "\n";echo '<p><span class="grey">Search:</span> <input type="text" name="search" class="search" /> <input type="submit" value="Search" class="button" /></p>'. "\n";echo '</form>'. "\n";echo '</div>'. "\n";subtitle($clientID); }/***************************************** * End Build Page Header Function*****************************************/?>Hopefully someone can find the error on these pages. Please help. Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-171528 Share on other sites More sharing options...
anatak Posted January 29, 2007 Share Posted January 29, 2007 mysqlconnect();there is nothing wrong with your way.Are you sure you did not make a typo error somewhere ? Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-171657 Share on other sites More sharing options...
tready29483 Posted January 29, 2007 Author Share Posted January 29, 2007 No, I posted everything as it is. pgheader doesn't work either. It says its undfined as well. I don't get whats wrong. Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-171870 Share on other sites More sharing options...
ted_chou12 Posted January 29, 2007 Share Posted January 29, 2007 *****unrelated******I have a suggestion for you. If this php page is a function that of your own, then is better to use require than include, since the php page with the function is essential for the php page to run.Hope that helpsTed Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-171873 Share on other sites More sharing options...
trq Posted January 29, 2007 Share Posted January 29, 2007 Why are you using this long url type path?[code=php:0]$functions = 'http://www.smoothestcreations.com/dev/demo/themes/'. $_SESSION['Theme'] .'/includes/functions.php';[/code]If functions is within the same directory just call...[code=php:0]$functions = 'functions.php';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-171874 Share on other sites More sharing options...
tready29483 Posted January 29, 2007 Author Share Posted January 29, 2007 No, its not in the same directory. In my first example it was. Regardless, should I make pgheader into a class since it calls two function inside of pgheader?? And if so what would it look like. The function should echo out the header html and build a menu and display the subtitle. Maybe I'm trying to get it to do too much. Not sure. Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-171885 Share on other sites More sharing options...
tready29483 Posted January 30, 2007 Author Share Posted January 30, 2007 Ok,I figured out what was wrong with my code. Apparently you can't do an include and use a full URL. I did not know that.On my index.php page i was calling the include like this:<?# Wrong Way$functions = 'http://www.smoothestcreations.com/dev/demo/themes/'. $_SESSION['Theme'] .'/includes/functions.php';include $functions;# Right Way$functions = 'includes/functions.php';include $functions;?>And now everything works fine. Mark this topic as SOLVED!!! Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-172417 Share on other sites More sharing options...
smc Posted January 30, 2007 Share Posted January 30, 2007 [quote author=tready29483 link=topic=124396.msg516720#msg516720 date=1170120481]Ok,I figured out what was wrong with my code. Apparently you can't do an include and use a full URL. I did not know that.On my index.php page i was calling the include like this:<?# Wrong Way$functions = 'http://www.smoothestcreations.com/dev/demo/themes/'. $_SESSION['Theme'] .'/includes/functions.php';include $functions;# Right Way$functions = 'includes/functions.php';include $functions;?>And now everything works fine. Mark this topic as SOLVED!!![/quote]ExactlyHere is a handy tip for you. At the top of your page do$root_path = '../';'../'; being subsituted for whatever takes you to get into your root path. ie. if your working in /root/directory, then you'll use ../, if your operating in root already you'll just use '';Then later on when you need to include something just typerequire ($root_path . 'functions.php');I use those a lot because I completly seperate my PHP and my HTML and they are in a whole host of different directoriesGood luck :) Quote Link to comment https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/#findComment-172457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.