sal Posted March 23, 2009 Share Posted March 23, 2009 My main file has 2 include files, database.php & functions.php. Database.php consists of variables holding the host name, password, db & username. Functions.php holds various functions. I tried moving some db related functions (connectDB, addData, etc.) from functions.php to database.php. But when I do this, the main file has a fatal error of undefined function, i.e. undefined function connectDB. However, the same functions run just fine when in functions.php. The error doesn't come from database.php not getting included, because the connection variables defined there work fine. Any ideas? Thanks. Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/ Share on other sites More sharing options...
Zhadus Posted March 23, 2009 Share Posted March 23, 2009 No reason off hand that it wouldn't work, please post the code using the code tags. Omit the connection variables. Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/#findComment-791815 Share on other sites More sharing options...
sal Posted March 23, 2009 Author Share Posted March 23, 2009 In main file: include_once("inc/database.php"); require("inc/functions.php"); ob_start(); if (isset($_POST['restart'])) { unset ($_POST['restart']); } $path = $_SERVER['PHP_SELF']; header($path, true); $blnSubmit = true; $dbConn = connectDB(); //undefined function if connectDB() in database.php In database.php <?php $host = "xx.x.x.x"; $db = "xxxxx"; $username = "xxxxx"; $password = "xxxxx"; ?> In functions.php (where it works) function connectDB() { $host = $GLOBALS['host']; $username = $GLOBALS['username']; $password = $GLOBALS['password']; $db = $GLOBALS['db']; $cn = new mysqli($host, $username, $password, $db); if (!cn) { throw new Exception("<strong>Unable to connect to database.</strong><br /> Please try again later?"); } else { return $cn; } } connectDB works when in functions.php, but not when in database.php. Other functions exihibit the same behavior. The variables in database.php work fine. Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/#findComment-791968 Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 Are you putting the database connection info before or after the function definition? Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/#findComment-791972 Share on other sites More sharing options...
sal Posted March 23, 2009 Author Share Posted March 23, 2009 I'm including database.php before requiring functions.php, and both come before the call to connectDB(), in the main file. Don't know if it makes a difference, but have tried include, include_once, require, require_once, etc. Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/#findComment-791984 Share on other sites More sharing options...
Zhadus Posted March 24, 2009 Share Posted March 24, 2009 From everything you have there, it almost has to be a typo type mistake. Post your changes and the exact error you receive. Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/#findComment-792564 Share on other sites More sharing options...
sal Posted March 24, 2009 Author Share Posted March 24, 2009 I don't see how it could be a typo, because I'm just cutting and pasting the function from one include file to the other. It works when in functions.php, but not in database.php. But who knows, obviously something wrong somewhere. The posted database.php file, by the way, is the entire file when the other functions aren't stored there. The error message is Fatal error: Call to undefined function connectDB() in C:\webs\customers\customer.php on line 22 //line 22 is the call to connectDB() in the main file Thanks again! Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/#findComment-792804 Share on other sites More sharing options...
Zhadus Posted March 24, 2009 Share Posted March 24, 2009 I'm asking you to show all the code, more than what you THINK is related. I've setup the same code on one of my pages, and it works perfectly fine. Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/#findComment-792910 Share on other sites More sharing options...
sal Posted March 25, 2009 Author Share Posted March 25, 2009 Well, ok Main file - Customer.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'> <head> <title>Customer</title> <!-- <link rel='stylesheet' type='text/css' href='css/quiz2.css'/> --> </head> <?php ("inc/database.php"); require("inc/functions.php"); ob_start(); if (isset($_POST['restart'])) { unset ($_POST['restart']); } $path = $_SERVER['PHP_SELF']; header($path, true); $blnSubmit = true; $dbConn = connectDB(); ?> <body> <div> <form name ='customer' method='post' action="<?php echo $_SERVER['PHP_SELF']; ?>"> <fieldset> <legend>Customer Data</legend> <table cellspacing='0' cellpadding='5'> <tr> <!-- ********** First Name ********** --> <td>First Name:</td> <td><input type='text' name='fname' value="<?php if (isset($_POST['fname'])) { echo trim($_POST['fname']); } ?>"> <span>*</span> </td> <td> <?php echo verify('fname'); ?> </td> </tr> <tr> <!-- ********** Last Name ********** --> <td>Last Name:</td> <td><input type='text' name='lname' value="<?php if (isset($_POST['lname'])) { echo trim($_POST['lname']); } ?>"> <span>*</span> </td> <td> <?php echo verify('lname'); ?> </td> </tr> <tr> <!-- ********** Street ********** --> <td>Street:</td> <td><input type='text' name='street' value="<?php if (isset($_POST['street'])) { echo trim($_POST['street']); } ?>"> <span>*</span> </td> <td> <?php echo verify('street'); ?> </td> </tr> <tr> <!-- ********** City ********** --> <td>City:</td> <td><input type='text' name='city' value="<?php if (isset($_POST['city'])) { echo trim($_POST['city']); } ?>"> <span>*</span> </td> <td> <?php echo verify('city'); ?> </td> </tr> <tr> <td>State:</td> <td><select name="state"> <OPTION value="0">-</option> <OPTION value="AK" selected>AK</option> <OPTION value="AL">AL</option> <OPTION value="AR">AR</option> <OPTION value="AZ">AZ</option> <OPTION value="CA">CA</option> <OPTION value="CO">CO</option> <OPTION value="CT">CT</option> <OPTION value="DC">DC</option> <OPTION value="DE">DE</option> <OPTION value="FL">FL</option> <OPTION value="GA">GA</option> <OPTION value="HI">HI</option> <OPTION value="IA">IA</option> <OPTION value="ID">ID</option> <OPTION value="IL">IL</option> <OPTION value="IN">IN</option> <OPTION value="KS">KS</option> <OPTION value="KY">KY</option> <OPTION value="LA">LA</option> <OPTION value="MA">MA</option> <OPTION value="MD">MD</option> <OPTION value="ME">ME</option> <OPTION value="MI">MI</option> <OPTION value="MN">MN</option> <OPTION value="MO">MO</option> <OPTION value="MS">MS</option> <OPTION value="MT">MT</option> <OPTION value="NC">NC</option> <OPTION value="ND">ND</option> <OPTION value="NE">NE</option> <OPTION value="NH">NH</option> <OPTION value="NJ">NJ</option> <OPTION value="NM">NM</option> <OPTION value="NV">NV</option> <OPTION value="NY">NY</option> <OPTION value="OH">OH</option> <OPTION value="OK">OK</option> <OPTION value="OR">OR</option> <OPTION value="PA">PA</option> <OPTION value="RI">RI</option> <OPTION value="SC">SC</option> <OPTION value="SD">SD</option> <OPTION value="TN">TN</option> <OPTION value="TX">TX</option> <OPTION value="UT">UT</option> <OPTION value="VA">VA</option> <OPTION value="VT">VT</option> <OPTION value="WA">WA</option> <OPTION value="WI">WI</option> <OPTION value="WV">WV</option> <OPTION value="WY">WY</option> </SELECT> </td> </tr> <tr> <!-- ********** Zip ********** --> <td>Zip:</td> <td><input type='text' name='zip' value="<?php if (isset($_POST['zip'])) { echo trim($_POST['zip']); } ?>"> <span>*</span> </td> <td> <?php echo verify('zip'); ?> </td> </tr> <tr> <!-- ********** Email ********** --> <td>Email:</td> <td><input type='text' name='email' value="<?php if (isset($_POST['email'])) { echo trim($_POST['email']); } ?>"> <span>*</span> </td> <td> <?php echo verify('email'); ?> </td> </tr> </table> </fieldset> <p></p> <input type='submit' value='Enter' id='submit' name = 'submit'> </form> <p>* = required field </p> </div> <p><?php displayData(); ?></p> </body> </html> database.php <?php $host = "xx.x.x.x"; $db = "xxxxx"; $username = "xxxxx"; $password = "xxxxx"; ?> functions.php <?php function connectDB() { $host = $GLOBALS['host']; $username = $GLOBALS['username']; $password = $GLOBALS['password']; $db = $GLOBALS['db']; $cn = new mysqli($host, $username, $password, $db); if (!cn) { throw new Exception("<strong>Unable to connect to database.</strong><br /> Please try again later, won't you?"); //echo "<strong>ERROR! Unable to connect to database.</strong><br /> Please try again later, won't you?"; //exit; } else { return $cn; } } function addCustomer($conn) { $fname = trim(addslashes($_POST['fname'])); $lname = trim(addslashes($_POST['lname'])); $street = trim(addslashes($_POST['street'])); $city = trim(addslashes($_POST['ciy'])); $state = trim(addslashes($_POST['state'])); $zip = trim(addslashes($_POST['zip'])); $query = "INSERT INTO myCustomer VALUES (null, '$fname', '$lname', '$street', '$city', '$state', '$zip')"; if (!mysqli_query($conn, $query)) { echo mysqli_error($conn); } else { echo "<p><strong>$fname $lname </strong><em>has been added to the database.</em></p>"; } } function retrieveData($conn) { $query = "SELECT * FROM myCustomer"; if (!mysqli_query($conn, $query)) { echo mysqli_error($conn); } else { $result = mysqli_query($conn, $query); echo "<table> <tr>"; echo "<td><strong>First Name</strong></td>"; echo "<td><strong>Last Name</strong></td>"; echo "<td><strong>Address</strong></td> </tr>"; while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { echo "<tr> "; echo "<td> ".$row['First_Name']."</td>"; echo "<td> ".$row['Last_Name']." </td>"; echo "<td> ".$row['Street'].' '.$row['City'].', '.$row['State'].' '.$row['ZipCode']." </td>"; echo "</tr>"; } echo "</table>"; } } function verify($data) { $errMsg = ""; if (isset($_POST['submit'])) { switch ($data) { case 'fname': $errMsg = validateNameCity(trim($_POST['fname']), 'first name'); break; case 'lname': $errMsg = validateNameCity(trim($_POST['lname']), 'last name'); break; case 'street': $errMsg = validateStreet(trim($_POST['street']), 'street'); break; case 'city': $errMsg = validateNameCity(trim($_POST['city']), 'city'); break; case 'state': $errMsg = validateStreet(trim($_POST['state']), 'state'); break; case 'zip': $errMsg = validateZip(trim($_POST['zip']), 'zip'); break; case 'email': $errMsg = validateEmail(trim($_POST['email']), 'email'); break; } } return $errMsg; } function validateNameCity ($value, $field) { $valid = ""; if (isset($value)) { if (!ereg("^[A-Z]{1}[a-zA-Z\ \-]{0,19}[a-zA-Z]{1}$", $value) || (FilterBadChar($value))) { if (empty($value)) { $valid = 'Please enter a '.$field.'.'; } else { $valid = $value.' is an incorrect '.$field.'. Please enter a correct '.$field.'.'; } $GLOBALS['blnSubmit'] = false; } return $valid; } } function validateStreet ($value, $field) { $valid = ""; if (isset($value)) { if (!ereg("^[[:digit:]]{1,6}\ [a-zA-Z0-9\-\.]", $value) || (FilterBadChar($value))) { if (empty($value)) { $valid = 'Please enter a '.$field.'.'; } else { $valid = $value.' is an incorrect '.$field.'. Please enter a correct '.$field.'.'; } $GLOBALS['blnSubmit'] = false; } return $valid; } } function validateState ($value, $field) { if (isset($value) && strlen($value) == '2') { $valid = ""; } else { if (empty($value)) { $valid = 'Please enter a '.$field.'.'; } else { $valid = $value.' is an incorrect '.$field.'. Please enter a correct '.$field.'.'; } $GLOBALS['blnSubmit'] = false; } return $valid; } function validateZip ($value, $field) { $valid = ""; if (isset($value)) { if (!ereg("^[[:digit:]]{5}", $value) || FilterBadChar($value)) { if (empty($value)) { $valid = 'Please enter a '.$field.'.'; } else { $valid = $value.' is an incorrect '.$field.'. Please enter a correct '.$field.'.'; } $GLOBALS['blnSubmit'] = false; } return $valid; } } function validateEmail ($value, $field) { $valid = ""; if (isset($value)) { if (!eregi("^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $value) || FilterBadChar($value)) { if (empty($value)) { $valid = 'Please enter an '.$field.' address .'; } else { $valid = $value.' is an incorrect '.$field.'. Please enter a correct '.$field.'.'; } $GLOBALS['blnSubmit'] = false; } return $valid; } } function FilterBadChar($value) { $badPattern = "[\'\=\<\>]"; if (ereg($badPattern, $value)) { return true; } else { return false; } } function displayData() { if ($GLOBALS['blnSubmit'] && isset($_POST['submit'])) { ob_clean(); $path = $_SERVER['PHP_SELF']; echo "<strong>Customer Information</strong>"; echo "<p>"; echo "<em>Name:</em><br />"; echo $_POST['fname']." ".$_POST['lname']."<br />"; echo "<em>Address:</em><br />"; echo $_POST['street']."<br />"; echo $_POST['city'].", ".$_POST['state']." ".$_POST['zip']; echo "</p>"; echo "<p><form name='ShowData' action='$path' method='post'>"; echo "<input type='submit' value='Restart' name='restart'>"; echo " "; echo "<input type = 'submit' value='Show All Customers' id='showall' name='showall' />"; echo "</form></p>"; $cn = $GLOBALS['dbConn']; addCustomer($cn); retrieveData($cn); } } ?> This configuration works. Cut & paste connectDB() (or other functions) from function.php to database.php and the undefined function error posted above occurs. Link to comment https://forums.phpfreaks.com/topic/150722-functions-in-include-files/#findComment-793526 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.