简体   繁体   中英

how can i include files for all functions in php

i'm new in php as you know and i have some problems (like any new starts) and my problem is i cant include file like config.php to all functions in the files

i have just 3 files

in config.php i just connect to database in mysqli and the variable is $connect

in functions.php there is just a simple functions

include('config.php');
function rows($table){
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

in index.php

include('functions.php');
echo rows('books');

and the problem is i cant get $connect variable from config.php to work in functions in functions.php file.

EDIT

when i include config.php inside the functions everything will be ok, like this

function rows($table){
include('config.php');
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

but i don't want to do this because i want to include a config.php for all functions i have.

Thanks

$connect exists in 'config.php'.

In your function(s), declare $connect as 'global' ie: it exists outside the scope of this function.

function rows($table){

    global $connect;

    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

Two possible ways are avaliable.

1.Passing $connect as a second parameter to the function

include('config.php');
function rows($table,$connnect){
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}
  1. Declaring $connect as a global variable

      function rows($table){ global $connect; $gettables = mysqli_query($connect,"SELECT * FROM $table"); $numrows = mysqli_num_rows($gettables); return($numrows); } 

It is recommended to pass $connect as a parameter to the function.Not recommended to use global variable refer here Are global variables in PHP considered bad practice? If so, why?

The problem is you declare a variable in the global scope, however whatever is available in the global scope is not automatically available in the function scope unless your force it available via global $connect which has its own caveats.

Some alternatives to global would be.

1) Use a singleton class:

<?php
//File connect.php
class Connection {
    private static $connection = null;
    public static get() {
          if (self::$connection == null) { self::$connection = mysqli_connect(...); }
          return self::$connection;
    }
} 

And use it as

include_once('connect.php');
function rows($table){
    $gettables = mysqli_query(Connection::get(),"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

2) Constants (Not the best idea):

 <?php 
 //File config.php
 define('CONNECTION',mysqli_connect(...));

Use as:

include_once('config.php');
function rows($table){
    $gettables = mysqli_query(CONNECTION,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

3) Pass it as a parameter to the function (config.php is as you have it now):

function rows($connection, $table){
    $gettables = mysqli_query($connection,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

Use as:

include('config.php');
include_once('functions.php');
echo rows($connection, 'books');

4) Have the file work like a variable declaration

<?php
//config.php 
return mysqli_connect(...);

Use as:

function rows($table){
     $connect = include('config.php');
     $gettables = mysqli_query($connect,"SELECT * FROM $table");
     $numrows = mysqli_num_rows($gettables);
     return($numrows);
}

you need to initiate a new object config inside your function and use the constructer to conect to database like this

$c=new config();
$con=$c->connect();
$req="SELECT * FROM table";
$conn=$con->query($req);

this works if you have done a class config in config.php

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