2

I have three .php files.

db_conx.php

$db_conx = mysqli_connect("localhost", "admin", "admin", "gestiune");
    // Evaluate the connection
    if (mysqli_connect_errno()) {
        echo mysqli_connect_error();
        exit();
}

functions.php

function deplata($pid){
        $sqlp = "SELECT * FROM plati WHERE user_id";
        $queryp = mysqli_query($db_conx, $sqlp);
        $type = "day";
        $payments = 0;
        $salary = 80;
        $days = 4;
        $topay = 0;
        while($getplata = mysqli_fetch_assoc($queryp)){
            $plati += $getplata['valoare'];
        }
        if($tip == "day"){
            $topay = $days * $salary;
        }

        return $topay;
}

And I have the index.php file to call the files and use them.

include_once("php_includes/db_conx.php");
include_once("php_includes/functii.php");

$salariu = deplata(5);
echo $salariu;

The problem is that it will not connect to the database and it returns some errors:

Notice: Undefined variable: db_conx in D:\xampp\htdocs\manager\pages\php_includes\functions.php on line 5

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in D:\xampp\htdocs\manager\pages\php_includes\functions.php on line 5

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in D:\xampp\htdocs\manager\pages\php_includes\functions.php on line 11

And then it prints the value (since it does not currently use database any values). What I tried: I tried to put the functions.php content directly into the index.php file, I tried to call the db_conx.php file inside the functions.php file.

3
  • Its not getting $db_conx you need to include that file within your functions.php Commented Mar 31, 2015 at 9:28
  • your sql statement isn't ok: "SELECT * FROM plati WHERE user_id=$pid" Commented Mar 31, 2015 at 9:32
  • I tried, this didn't helped. What solved the problem was what @Developing suggested: global $db_conx; Thanks for your time! Commented Mar 31, 2015 at 9:32

1 Answer 1

3

You need to define the value the in function.php.checkout the below code

function.php

function deplata($pid){

   global $db_conx;

    $sqlp = "SELECT * FROM plati WHERE user_id";
    $queryp = mysqli_query($db_conx, $sqlp);
    $type = "day";
    $payments = 0;
    $salary = 80;
    $days = 4;
    $topay = 0;
    while($getplata = mysqli_fetch_assoc($queryp)){
        $plati += $getplata['valoare'];
    }
    if($tip == "day"){
        $topay = $days * $salary;
    }

    return $topay;
}

index.php

Check the typo mistake in index.php

include_once("php_includes/function.php");
Sign up to request clarification or add additional context in comments.

2 Comments

Hello! Thank you for helping me. I'm a beginner with functions, this helped. I will mark this as winning answer as soon as the website will allow me. Again, thanks!
The typos were there because I translated the variables and names to English in the post, so you guys can find the logic in my script. In my files they're in Romanian.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.