Jump to content

[SOLVED] Call function within another function


programguru

Recommended Posts

Create a separate file called dbconnect.php

 

Put this in your dbconnect.php file.

 

 

<?php

        function db_connect()
{

        Global $host, $username, $password, $dbname, $link_id, $table_name;
        
        $host = "localhost";
        $username = "xxxxx";
        $password = "xxxxx";
        $dbname = "xxxxx";
        $table_name = "xxxxx";
        
        $db = mysql_connect($host,$username,$password);
        if (!$db)
        {
        echo "connect: mysql_error()";
        exit;
        }
        
        $select_db = mysql_select_db($dbname,$db);
        if (!$select_db)
        {
        echo "select: mysql_error()";
        exit;
        
        return ($link_id);
        
        }
  
    }    
  
?>

 

 

Now in your main program include the dbconnect.php file.

 

 

include_once("dbconnect.php");
$link_id = db_connect();

 

 

Now run your function:

 

 

function viewSpecialsUi() {
  $link_id = db_connect();
  $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);
}

 

 

Now do me a favor and just fill in $table_name manually just to get the whole process working. Then we can work on

passing that variable over to the function....

 

 

 

 

Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

The simplest way (without creating a class) of doing it would be to define your connection details within constants. Constants are global in scope.

 

settings.php

<?php

define('DBNAME', 'foo');
define('DBHOST', 'foo');
define('DBUSER', 'foo');
define('DBPASS', 'foo');
define('DBTABLE', 'foo');

?>

 

functions.php

<?php

require_once 'settings.php';

function dbConnect() {
  mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error());
  mysql_select_db(DBNAME) or die(mysql_error());
}

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

?>

 

Having said that. Unless you plan on using one table for your entire application I don't really see much point. Also, you realise your viewSpecialsUi() doesn't actually serve any purpose? I take it its just an example.

Link to comment
Share on other sites

The simplest way (without creating a class) of doing it would be to define your connection details within constants. Constants are global in scope.

 

settings.php

<?php

define('DBNAME', 'foo');
define('DBHOST', 'foo');
define('DBUSER', 'foo');
define('DBPASS', 'foo');
define('DBTABLE', 'foo');

?>

 

functions.php

<?php

require_once 'settings.php';

function dbConnect() {
  mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error());
  mysql_select_db(DBNAME) or die(mysql_error());
}

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

?>

 

Having said that. Unless you plan on using one table for your entire application I don't really see much point. Also, you realise your viewSpecialsUi() doesn't actually serve any purpose? I take it its just an example.

 

Hi Thorpe,

 

Well I really appreciate your help. But that's not working for me either. I did not post up all of my code.. there are query results etc below what I have that echo out.

 

I tested my script with my details direct in the viewSpecialsUi() function and all works perfect. So it's just something that's not coming together for whatever reason :(

Link to comment
Share on other sites

Once I figure out something that works, I will post it up.

 

The code I posted (using constants) should work. If yours doesn't youv'e done something wrong, post your code.

 

here is what i tried with these:

 

define('DBNAME', 'xxxxxx');

define('DBHOST', 'xxxxxx');

define('DBUSER', 'xxxxxx');

define('DBPASS', 'xxxxxx');

define('DBTABLE', 'xxxxxx');

 

function dbConnect() {

  $connection = mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error());

  $db = mysql_select_db(DBNAME) or die(mysql_error());

}

 

 

function viewSpecialsUi() {

dbConnect();

$bd_string = "<ul>";

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

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

$num = mysql_num_rows($result);

 

if ($num < 1) {

echo "<p>There are no records to display.</p>";

} else ...

Link to comment
Share on other sites

This works perfect, and my results are printed OK FYI:

 

function viewSpecialsUi() {

//dbConnect();

$db_name = "xxxxxxxx";

$connection = mysql_connect("localhost", "xxxxxxxx", "xxxxxxxx") or die(mysql_error());

$db = mysql_select_db($db_name, $connection) or die(mysql_error());

$table_name = "xxxxxxxx";

$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);

 

if ($num < 1) {

echo "<p>There are no records to display.</p>";

} else .....

 

 

 

<?php viewSpecialsUi(); ?>

Link to comment
Share on other sites

In the code where you are using constants (two posts up) you are still referencing $connection in this line....

 

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

 

For about the fifth time. This variable does not exist within this function!!!!

 

Remove it.

Link to comment
Share on other sites

In the code where you are using constants (two posts up) you are still referencing $connection in this line....

 

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

 

For about the fifth time. This variable does not exist within this function!!!!

 

Remove it.

 

If it's going to make you crazy "!!!!!!" I think it's best if I figure this out on my own. And you defined $connection = mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error()); in you sample, so how does $connection not exist?

Link to comment
Share on other sites

And you defined $connection = mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error()); in you sample, so how does $connection not exist?

 

I think if you actually look you will see I did not define $connection within dbConnect(), and even if I did, it would only exist within dbConnect().

 

The simple thing is this. Variables created within functions only exist within that same function. No where else.

Link to comment
Share on other sites

I really see what  you mean now on the limitations of the scope.

 

I just don't see why this would not work:

 

$table_name = "xxxxxx";

function dbConnect() {

$db_name = "xxxxxxxxxx";

$connection = mysql_connect("localhost", "xxxx", "xxxx") or die(mysql_error());

$db = mysql_select_db($db_name, $connection) or die(mysql_error());

global $table_name;

}

 

Shouldn't $table_name be accessible in my other function when dbConnect() is called?

Link to comment
Share on other sites

The last version of your code was close but missing a key detail:

 

Function dbConnect() you make a variable $connection but you never pass this variable back using:

 

return $connection;

 

In the viewSpecialsUi function you need to call the function like so:

 

$connection = dbConnect();

 

 

Try this.  If it does not work, then in the dbConnect() function do the return like this:

 

return &$connection;

 

Link to comment
Share on other sites

The last version of your code was close but missing a key detail:

 

Function dbConnect() you make a variable $connection but you never pass this variable back using:

 

return $connection;

 

In the viewSpecialsUi function you need to call the function like so:

 

$connection = dbConnect();

 

 

Try this.  If it does not work, then in the dbConnect() function do the return like this:

 

return &$connection;

 

 

This solution was posted several posts ago. I'm not sure the OP is reading the solutions provided.

Link to comment
Share on other sites

Thanks thorpe,

The last version of your code was close but missing a key detail:

 

Function dbConnect() you make a variable $connection but you never pass this variable back using:

 

return $connection;

 

In the viewSpecialsUi function you need to call the function like so:

 

$connection = dbConnect();

 

 

Try this.  If it does not work, then in the dbConnect() function do the return like this:

 

return &$connection;

 

 

This solution was posted several posts ago. I'm not sure the OP is reading the solutions provided.

 

Thanks I will try that. I created this test script just to see if I could get my global to kick and NO luck:

 

// please let me know if this is working for you!

 

$table_name = "This is the output of a global!";

 

function dbConnect() {

  global $table_name;

  echo "$table_name";

}

 

function globalEcho() {

  echo "$table_name";

}

 

globalEcho(); // <-- this does not output!

 

dbConnect();  // <-- this outputs!

Link to comment
Share on other sites

I'm tracking with you. I just am trying to figure out why it's not working to eliminate possibilities. I tried both options you presented and neither of them worked. Same error:

 

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\www\htdocs....

 

If your going to start using globals don't bother using functions. Most of the encapsulation is out the window. The entire point of functions is to ecapsulate code and not to poison your global namespace with variables.

Link to comment
Share on other sites

I'm with Thorpe. I was referring to the version you posted where you used the Constants for the connection details, and defined the two functions.  Please go back to that, as it is the most sensible code you've posted, and very close to working.  Globals aren't going to make things better at this point.

Link to comment
Share on other sites

I'm with Thorpe. I was referring to the version you posted where you used the Constants for the connection details, and defined the two functions.  Please go back to that, as it is the most sensible code you've posted, and very close to working.  Globals aren't going to make things better at this point.

 

The problem is the constants function was not working either. I tried many variations with no luck.

 

I hate to tie up the forum with my problem. But I'm just on a relentless battle now to figure out something that works, so I don't have to have a redundant $table_name in all of my functions when I could just call one dbConnect() function that holds it.

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.