Jump to content

[SOLVED] Call function within another function


programguru

Recommended Posts

hello,

i cannot get past this. i have attempted many variations of including this function within my other function, and i cannot get it to work.

 

// database connection
function dbConnect() {
$db_name = "xxxxxxxxxx";
$connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
$table_name = "xxxxxxxxxx";
}

 

The function i am trying to use to include the above function:

 

function viewSpecialsUi() {
dbConnect(); // <<-- here is the function I am trying to include
$bd_string = "<ul>";
$sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added";
$result = mysql_query($sql, $connection) or die(mysql_error());
$num = mysql_num_rows($result);

...

 

I've aleady confirmedmy script is working by including the connection details within the viewSpecialsUi()  function, but it just won't work as I would like it to.

Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply

I think I know why this is- you need to define all variables for a function when calling it.

eg-

function foo()
{
connect('localhost', 'user', 'pass');
#...rest of function...
}

Functions don't reference global variables.

Well, I don't think they do.

Hope this helps.

Jack.

Link to comment
Share on other sites

hmmm.. i tested your script on my php settings and it worked OK... I cannot get away from this:

 

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\www\htdocs\xxxxxxxxxx.com\xxxxxx\xxxxx\phpFunctionsUi.php on line 44

 

 

 

Right ok, make a change to your DB function, use this:

 

 

// database connection
function dbConnect() {
$db_name = "xxxxxxxxxx";
$connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
$table_name = "xxxxxxxxxx";

return ($connection); <---add this

}

Link to comment
Share on other sites

I also created a class, and it did not work either...

 

1) So your script worked A-OK! Which means functions CAN be called within functions..

 

2) I am able to run my script perfect placing the connection details directly in the viewSpecialsUi() function.

 

But there seems to be some issue with calling these connection details in a function  (maybe a security issues?????)

 

Still stuck!

SOS

Link to comment
Share on other sites

Right ok, make a change to your DB function, use this:

 

 

// database connection
function dbConnect() {
$db_name = "xxxxxxxxxx";
$connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
$table_name = "xxxxxxxxxx";

return ($connection); <---add this

}

 

Hmmm.. nope did not work on that..

Link to comment
Share on other sites

Ok well try this now...

 

 

 

 


// database connection
function dbConnect() {

Global  $connection; <---add this make it global

$db_name = "xxxxxxxxxx";
$connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
$table_name = "xxxxxxxxxx";

return ($connection); <---add this

}

Link to comment
Share on other sites

Don't use globals within functions. Remove any reference to $connection from within viewSpecials() as that variable does not exist.

 

function viewSpecialsUi() {
  dbConnect();
  $bd_string = "<ul>";
  $sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added";
  $result = mysql_query($sql) or die(mysql_error());
  $num = mysql_num_rows($result);
}

 

or, have dbConnect() return $connection and use it like....

 

function viewSpecialsUi() {
  $conn = dbConnect();
  $bd_string = "<ul>";
  $sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added";
  $result = mysql_query($sql, $conn) or die(mysql_error());
  $num = mysql_num_rows($result);
}

Link to comment
Share on other sites


// database connection
function dbConnect() {

Global  $connection; <---add this make it global

$db_name = "xxxxxxxxxx";
$connection = mysql_connect("localhost", "xxxxxxxxxx", "xxxxxxxxxx") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());
$table_name = "xxxxxxxxxx";

return ($connection); <---add this

}

 

did this work for you? did not work on my end...

 

I just tested a simple echo function within my script and the output was OK..

 

it's got to be related to the database connection details specifically and some restriction within php... but there has to be a way..

 

still seeking.

Link to comment
Share on other sites

OK I think now I really found it, you are trying to use

$table_name

variable in you second function, but it is not even declare in this function.

 

You will have to create an array containing $connection and $table_name in your first function and then:

 

retrun $myarray

 

See if it works.

Link to comment
Share on other sites

You really need to read up on variable scope and how it relates to functions. Variables created within functions are not available outside of that function. That is almost the entire point of functions, to encapsulate code.

Link to comment
Share on other sites

You really need to read up on variable scope and how it relates to functions. Variables created within functions are not available outside of that function. That is almost the entire point of functions, to encapsulate code.

 

I do understand encapsulation and why it exists, but the code you posted does not work. That is what I was using prior on my first attempts:

 

function viewSpecialsUi() {

  dbConnect();

  $bd_string = "<ul>";

  $sql = "SELECT id, date, date_added, title, image FROM $table_name ORDER by date_added";

  $result = mysql_query($sql) or die(mysql_error());

  $num = mysql_num_rows($result);

}

Link to comment
Share on other sites

OK I think now I really found it, you are trying to use

$table_name

variable in you second function, but it is not even declare in this function.

 

You will have to create an array containing $connection and $table_name in your first function and then:

 

retrun $myarray

 

See if it works.

 

I did define $table_name = "special"; in the dbConnect() function??? I am totally missing the point?

Link to comment
Share on other sites

As I said, variables declared within a function are only available within that function. Meening that $table_name only exists within dbConnect().

 

I am totally missing the point?

 

Indeed.

 

thorpe,

 

I appreciate your assistance. And I am glad there are forums like this.

 

I'm just not getting how to globally use the var if I cannot use global to give it proper scope.

 

Let me know if you come across the answer.

 

I'm going to keep searching.

Link to comment
Share on other sites

 

 

Dude, stop messing around with variables, let's just get your query to work. Statically fill in what table_name you want!!

 

 

$sql = "SELECT id, date, date_added, title, image FROM [b]MYTABLENAME[/b] ORDER by date_added";

 

You can make it a variable later after it works...

 

 

allworknoplay,

 

the whole point is i want to keep all of the connection details contained in one location for "class" like usage.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • 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.