Jump to content

Recommended Posts

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 functions
include('functions.php');

# Call Db connections
mysqlconnect();
?>

Fatal error: Call to undefined function: mysqlconnect() on Line

Can anyone see what i am doing wrong or give me an example of a working linked function page??



Link to comment
https://forums.phpfreaks.com/topic/36048-solved-newbie-question-about-functions/
Share on other sites

Hi mate,

Use this...

[code]
<?php

function 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]
<?php
include_once('functions.php'); // This Connects To The MySQL Database

ConnectDatabase();

?>
[/code]
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>&nbsp;&nbsp; <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.
*****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 helps
Ted
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]
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.
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 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]

Exactly

Here 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 type

require ($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 directories

Good luck :)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.