INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

DB Connection in Function Call

DB Connection in Function Call

(OP)
I have a page that is dedicated to record maintenance. I have 3 tables where 1 is the main record and the other 2 are multiple records attached to a single record in the main. IE. cstOrders, orderParts, orderServices. When any of the 3 tables get changed (parts added, deleted or edited) I want created a function to calculate the total. My issue is that once it gets to the function it throws the error...

Notice: Undefined variable: conn in /var/www/html/myprogram/maint.php on line 9

I have on line 2 which connects to the database,

CODE --> php

include "db_connect.php"; 
.
This include works flawlessly until now. Is there some reason why I am getting the Undefined variable for my connection?

RE: DB Connection in Function Call

Hi

Sounds like a scope issue. In PHP the functions can not access global scope by default, only on special request ( see the global keyword ) :

CODE --> PHP

<?php

include "db_connect.php";

function whatever($str)
{
    global $conn;

    $conn->prepare($str);
    // ...
} 

But hard to guess without seeing more code, specially the variable and function declaration.

Feherke.
feherke.github.io

RE: DB Connection in Function Call

(OP)
That did the trick. Thank you.


Since I am using the connection in the include is there a drawback to changing "$conn" to "global $conn" in the file?

RE: DB Connection in Function Call

Hi

You mean to add global $conn; directly in db_connect.php so you not have to add it in each function that will use $conn ? No, it not works like that. The global keyword is not like the public keyword, it not modifies the visibility of the enumerated variables; they are already global anyway if you declared them in the global scope. The keyword says that the enumerated variables instead of referring to the current scope's variables, will refer to the global ones. So you have to do this again and again when inside a function need global variable.

Alternatively you may use the $GLOBALS superglobal.

Feherke.
feherke.github.io

RE: DB Connection in Function Call

(OP)
Good to know. Thanks for the lesson!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close