PHP and MySQL Nested database connection -


this general example. application has 3 files.

conn.inc.php -- setting database connection

<?php $db_host = "localhost"; $db_username = "root"; $db_pass = ""; $db_name = "hmt";  $conn = new mysqli($db_host, $db_username, $db_pass, $db_name); if ($conn->connect_error) {   die("connection failed: " . $conn->connect_error); }  ?> 

func.inc.php -- file including functions

<?php function load_module($module_name){   $sqlcmd = "select content modules name='$module_name' limit 1";   $result = $conn->query($sqlcmd);   if ($result->num_rows > 0) {     while($row = $result->fetch_assoc()) {       $module_footer = $row["content"];     }   }else {     echo 'error while loading module '.$module_name;   }   return $result;   $result->free_result(); } ?> 

index.php -- main page display content

<?php include 'conn.inc.php'; include 'func.inc.php';  if (!isset($_get['page_name'])) { // if page_name not set reset homepage   $page_name = 'module_footer'; }else{   $page_name = $_get['module_footer']; } $module_content = load_module($page_name); echo $module_content; ?> 

now goal include functions inside func.inc.php file , database conn.inc.php, keep separate , easier read in future.

my problem $conn variable declared in conn.inc.php cannot used inside function , can't head around how use it. tried using globals no success.

the error files this:

notice: undefined variable: conn in ./func.inc.php on line 4  fatal error: call member function query() on non-object in ./func.inc.php on line 4 

which (i assume) because $conn variable not in global scope.

now question is. how can keep nested files have functions working? there mistake in approach or not possible use nested call mysql object?

eventually you'll want object-oriented coding lets make have little prettier.

when including files, you'll want avoid things global variables. sound great, end being pain when handling scope. instead include set of functions call.

conn.inc.php

function getconnection(){     $db_host = "localhost";     $db_username = "root";     $db_pass = "";     $db_name = "hmt";      $conn = new mysqli($db_host, $db_username, $db_pass, $db_name);     if ($conn->connect_error) {       die("connection failed: " . $conn->connect_error);     }      return $conn; } 

then function.inc.php

<?php function load_module($module_name){   $sqlcmd = "select content modules name='$module_name' limit 1";   //here our database connection.   $conn = getconnection();   $result = $conn->query($sqlcmd);   if ($result->num_rows > 0) {     while($row = $result->fetch_assoc()) {       $module_footer = $row["content"];     }   }else {     echo 'error while loading module '.$module_name;   }   return $result;   $result->free_result(); } 

and finish index page way is. now, page wants database connection need include conn.inc.php , call getconnection() mysqli connection object.

think of program being series of individual functions working together. , you'll being series of objects working together. nothing should floating off in global space. try encapsulate in sort of function or object called on , on consistent results.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -