Function works when called from its own PHP file. Does not work when included on another php file

I have a quandary. I created a custom function(f_TermLevelStatList()) to build and display a high level stats table. Instead of clogging my main PHP file (class_list.php), I built it in a separate file (func_highlevel_statlist.php) that I am including via include_once.

If I run the func_highlevel_statlist.php file in the browser by calling the function f_TermLevelStatList() it works perfectly and displays the table.

When I run the class_list.php file I get the following error message.

Fatal error: Uncaught Error: Call to a member function query() on null in /volume1/web/gh-web/func_highlevel_statlist.php:5 Stack trace: #0 /volume1/web/gh-web/func_highlevel_statlist.php(46): f_TermLevelStatList() #1 /volume1/web/gh-web/class_list.php(2): include_once(’/volume1/web/gh…’) #2 {main} thrown in /volume1/web/gh-web/func_highlevel_statlist.php on line 5

How can I get this to work via include_once?

here is the code of class_list.php:

<?php include_once "./gh_config/db_conn.php";
	include_once "func_highlevel_statlist.php";
$sqlListSubCatDesc =
    "SELECT DISTINCT `subject`, `catalog`, `description`  FROM barpsc.class_tbl " .
    " WHERE `subject` Not IN ('PERM') Order By `subject` ASC, `catalog` ASC;";



?>

<!DOCTYPE HTML5>
<html>
<head>
<style>
	table {
	width:100%;
	}
</style>
<title> Class By Class Enrollment Number Listing </title>
</head>
<body>
<?PHP 
	f_TermLevelStatList();
?>
</body>
</html>

Here is the code for func_highlevel_statlist.php:

<?PHP 
	function f_TermLevelStatList(){
	include_once "./gh_config/db_conn.php";
	$sql = "SELECT * FROM barpsc.VW_NumSections_ALL_STATS;";
	$Num_Sec_All_Stat = $PDO->query($sql);
?>
	<div id="SectStatsAllByTerm">
	<h2> Section Stats Summer 2014 to Spring 2023</h2>
	<h3> As of Friday, November 18, 2022 </h3>
	
	<table class="TermLevelStats" style="border-collapse:collapse;padding:2pt;">
		<thead>
			<tr style='border-bottom:2pt solid blue;'>
				<th style='padding: 5px;'>Term</th>
				<th style='padding: 5px;'>All Sections</th>
				<th style='padding: 5px;'>Instructor Sections</th>
				<th style='padding: 5px;'>Non-Instr. Sections</th>
				<th style='padding: 5px;'>Active</th>
				<th style='padding: 5px;'>Cancelled</th>
				<th style='padding: 5px;'>Tentative</th>
				<th style='padding: 5px;'>Stop Enrl</th>
			</tr>
		</thead>
		<tbody>

<?PHP
//display body of table
	while ($row_num_sec_all_stat = $Num_Sec_All_Stat->fetch()){
		echo "<tr style='border-bottom:1pt solid blue;'>\n";
		echo "<th scope='row' style='text-align:right;'>".$row_num_sec_all_stat['Term']."</th>\n";
		echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_ALL']."</td>\n";
		echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_INSTR']."</td>\n";
		echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_NONINSTR']."</td>\n";
		echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_ACTIVE']."</td>\n";
		echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_CANCELLED']."</td>\n";
		echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_TENTATIVE']."</td>\n";
		echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_STOP_ENRL']."</td>\n";
		echo "</tr>\n";		
	}
?>
	</tbody>
	</table>
	</div>
<?PHP
}
?>

Did you do this -

from the previous thread? You would be getting an undefined variable error for $PDO that might have helped you determine why this isn’t working.

The code for any page should be laid out in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

You should make one database connection in the initialization section, then supply it to any function that needs it as a call-time parameter. Also, use ‘require’ for anything your code must have for it to work. Don’t bother with the _once() versions of require/include.

I recommend that you separate the database specific code, that knows how to query for and fetch the data (item #3 in the list), from the presentation code, that knows how to produce the output from the data (item #4 in the list). The way to do this is to fetch all the data from the query into an appropriately named php variable, then test (so that you can output a message if the query didn’t match any data) and loop over this variable at the appropriate location in the html document.

Don’t write out verbose variable names for nothing. This whole section of code is to get and display the ‘sections all statistics’. Everything in it, until the next section, that would have a major comment stating so, is for getting and display the ‘sections all statistics’. Just name the PDOStatement object $stmt or similar and name the fetched row of data $row or similar.

Your previous suggestions worked and I had the page working.

I am not working with a form, I am just working with an existing dataset and trying to make it human readable.

Regarding the longer names, because I was going to be using nested queries where the first query pulled a list of categories and the second queries was pulling related components with a third that was going to pull the data to display in the tables. I was using the longer names to help me know what I was working with.

I figured out passing the $PDO variable to get the function to work. I just dont understand why when I did the include inside the function it did not work.

Use a single join query.

Because you had already included the file in the main code, the second include_once didn’t do anything.

Sponsor our Newsletter | Privacy Policy | Terms of Service