2

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

Dileep Kumar
  • 965
  • 7
  • 13
Mohammed Alhanafi
  • 878
  • 1
  • 9
  • 22
  • try require_once instead of include – Exprator Jul 07 '17 at 07:41
  • 3
    Either move to classes, pass the variable `connect` to the function instead of including it or make use of the `global` keyword – DarkBee Jul 07 '17 at 07:41
  • @Exprator i tried it, not worked. – Mohammed Alhanafi Jul 07 '17 at 07:52
  • Including files in functions can bring unexpected errors. The inclusion happens at runtime and this means the included file is loaded and parsed every time the function runs. This produces problems if the include file contains things that cannot be declared twice (defines, functions, classes). – axiac Jul 07 '17 at 07:57
  • @axiac Thanks dude, i just use global here to connect database, in other time i will not forget your words ;). – Mohammed Alhanafi Jul 07 '17 at 08:24

4 Answers4

4

$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);
}
MaggsWeb
  • 2,912
  • 1
  • 11
  • 20
4

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?

Arun Kumaresh
  • 5,734
  • 5
  • 28
  • 45
  • Thanks for the info dude, i will use global because my codes are simple, in other project i will not forget your words :), Thanks again – Mohammed Alhanafi Jul 07 '17 at 08:30
3

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);
}
apokryfos
  • 30,388
  • 6
  • 55
  • 83
0

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