Jump to content

[SOLVED] Undefined variable with require_once


isaac_cm

Recommended Posts

Hello,

I get the following notice whenever I try to call a function from my file :

Notice: Undefined variable: database_mainsqldb in F:\mysite\Site Template\includes\myfunc.php on line 7

 

this variable "$database_mainsqldb" is defined in this file "../Connections/mainsqldb.php" and I include this file like that:

<?php require_once('../Connections/mainsqldb.php'); ?>

 

I tried to use the same function in the same calling page and it work fine without the previous notice!! the error appear only when I try to call it from myfunc.php

 

here is the script of myfunc.php

function delete_attr($aid){

  mysql_select_db($database_mainsqldb, $mainsqldb);

  $deleteSQL = "DELETE FROM table1 WHERE at_id=" . $aid;

  $Result1 = mysql_query($deleteSQL, $mainsqldb) or die(mysql_error());

 

}

 

plz advice

thanks

Link to comment
Share on other sites

I suggest you loop up variable scope and functions. To start with, try reading through this section from a free online php book:

http://hudzilla.org/phpwiki/index.php?title=Variable_scope_in_functions

 

Basically, variables declared outside of a function aren't available inside a function unless you pass them into it as a parameter, or use the global keyword.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.