简体   繁体   中英

db connection not establish for function

I have dbconnect.php file to use all my php files eg., index.php etc.. Also i have functions.php file having all the functions. I always getting nothing when i call a function.

setparentid_new($studentid) is a function located in "/functions/functions.php" which path already included in index.php. all the functions is not working because of db connection is not there. If i include dbconnect.php in all the functions then only return the results.

$parentid is hanging, so below all code not executing.

$con not available in functions. In all functions $con need to decalre. Please help to pass $con value to each functions globally.

dbconnect.php:

<?php 

define("mysqli_HOST","localhost"); 
define("mysqli_USER","root"); 
define("mysqli_PASSWORD",""); 
define("mysqli_DB","schooldb"); 

$con=mysqli_connect(mysqli_HOST,mysqli_USER,mysqli_PASSWORD,mysqli_DB) or die("Unable to connection. Check connection parameters"); 

?>

index.php:

<?php 

session_start(); 
ini_set('display_errors', 0); 

include $_SERVER['DOCUMENT_ROOT'] . "/dbs/dbconnect.php"; 
include $_SERVER['DOCUMENT_ROOT'] . "/functions/functions.php"; 
?> 
<html>
  <div id="bodycontainer"> 
    <?php $insertSQL="INSERT INTO newstudent(col1,col2)values ('data1','data2')"; $result=mysqli_query($con,$insertSQL) or die(mysqli_error()); mysqli_query($con,"UPDATE newstudent SET status ='A' WHERE studid=$studid"); $parentid=setparentid_new($studid); ------- ------ ------ ?> 
</html>

setparentid_new() function:

setparentid_new($studid) { 
   $sql    = "SELECT * FROM parentsmaster WHERE childid=$studid"; 
   $result = mysqli_query($con,$sql) or die(mysqli_error()); 
   $count = mysqli_num_rows($result); 

   if($count==1) { 
     $mynewarray = array(); 
     foreach ($mynewarray as $key => $row) { 
       $array_depth[$parentid] = $row['parentid']; 
     } 
   } 
}

I'm not so sure about your structure, but as I understand from your comment, the solution is to define a global variable in index.php and than assign him the db connection.

index.php file:

<?php

    $settings = new stdClass;

    require ("db_connection.php");

    require ("functions.php");

    echo "Hello World!";

db_connection.php file example, note the global $settings call:

function db_con($config) {
    global $settings; // This is how you should call the global $settings var inside the functions.

    $settings->db = mysqli_connection...
}

Use the same global call in functions.php file's functions as well.

You can try in two ways:

  1. Declaring in the function.php a variable connection then you can pass it by a getter function

    --function.php-- $con; function getCon() { global $con; return $con; } --other page-- include('function.php'); function a(){ 1.$con=getCon(); 2.global $con }
  2. Set the connection in the session scope, that allows you to pass the connection between the pages.

     set/update $_SESSION["con"]=$con; get $con=$_SESSION["con"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM