4 Replies - 380 Views - Last Post: 01 November 2013 - 10:15 PM Rate Topic: -----

#1 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 156
  • Joined: 07-October 12

Function doesn't use include file.

Posted 01 November 2013 - 06:00 PM

I'm pretty sure that my issue is that it's not actually including the pre-header.php file which has my database variable for $con because if I write in my connection string above $result it works. Is there anyone that knows why the function doesn't carry the include file?


<?php
include '../pre-header.php';


	function GetNavMenu(){
	print '<a href="index.php" class="MainNavMenu">Home</a>';
	$result=mysqli_query($con,"SELECT * FROM pages WHERE main_nav=1");
	while($row=mysqli_fetch_array($result))
		{

			print "<a href='viewpage.php?id=" .$row['id']."' class='MainNav'>".$row['nav_title']."</a>";
		
		}
	}
//The Function Works halfway. When I use GetNavMenu() In the page I'm using it in. It will give me the Home Page, but not the SQL Generated Pages Below

?>



Is This A Good Question/Topic? 0
  • +

Replies To: Function doesn't use include file.

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3706
  • View blog
  • Posts: 5,946
  • Joined: 08-June 10

Re: Function doesn't use include file.

Posted 01 November 2013 - 06:23 PM

In your mysqli_connect call you use a $con variable. That variable doesn't exist in that scope.

Functions exist in their own scopes, separate from the global scope. So if you define $con in your header file as a MySQLi connection reference, then using $con inside a function won't work. It has to be imported into that scope for it to be usable. Defining it in the global scope is not enough.

You can import it in several ways. I'll list them in order of my highest preference.

  • If using a decent framework, create a service. You're obviously not using a framework, so this is hardly applicable here. Worth mentioning though.

  • Use a singleton; a class that creates a static instance of the resource in question, and then provides a static getter method that initializes the instance if it's hasn't been already, and then returns it. That way all your code can access that resource through that static method without re-creating it.

  • Inject the global variable into your function through a parameter. For example.
    // Create MySQLi connection globally.
    $conn = new mysqli(...);
    
    // Define function to take MySQLi instance as parameter.
    function fooBar($conn) {
        $conn->...
    }
    
    // Pass MySQLi connection when function is called.
    fooBar($conn);
    
    


  • Import the global variable into the function using global. - I'd recommend avoiding this option. It's never good to rely on global variables like this. Better to pass things through parameters.
    // Create MySQLi connection globally.
    $conn = new mysqli(...);
    
    // Define function to import the global $conn.
    function fooBar() {
        global $conn;
        $conn->...
    }
    
    // Call function without anything extra for MySQLi.
    fooBar();
    
    

Was This Post Helpful? 0
  • +
  • -

#3 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 156
  • Joined: 07-October 12

Re: Function doesn't use include file.

Posted 01 November 2013 - 06:37 PM

So would a global include work by chance?
IE
<?php
$var = include '../pre-header.php';
function GetNav($var) {
         mysqlconnectionandquery
         while($row=($result))
                 {
                  print $row[myrow];
                  }
    }


Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3706
  • View blog
  • Posts: 5,946
  • Joined: 08-June 10

Re: Function doesn't use include file.

Posted 01 November 2013 - 08:09 PM

That $var declaration doesn't make any sense in this context. Unless the included PHP file returns something it won't have anything worth assigning to a global variable. That also doesn't eliminate the core problem: the scope. $var there is still just as global as the original $conn variable declared in the included file.

The function operates in an entirely different scope. That includes the parameters. You need to pass the global variable into the function through a parameter, as I demonstrated in point 3, or import it using the global keyword, as I demonstrated in point 4.
Was This Post Helpful? 0
  • +
  • -

#5 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 156
  • Joined: 07-October 12

Re: Function doesn't use include file.

Posted 01 November 2013 - 10:15 PM

Kind of a bummer. I have everything defined within a config file that I was hoping to import to all my functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1