Jump to content

passing variables between functions


mrbean

Recommended Posts

I have a little problem.

I have made a script(function) to connect to the database.

this is the script:

connect(php)

<?php
$server = "localhost";
$username = "username";
$password = "password";

function connectdatabase($type,$database)
{
    if($type == "mysql")
    {
    $mysql = mysql_connect($server, $username, $password);
    mysql_select_db($database, $mysql);
    }
    else if($type == "mysqli")
    {
    $mysqli = new mysqli($server, $username, $password, $database);
    }
    else if($type == "mssql")
    {
    $mssql = mssql_connect($server, $username, $password);
    mssql_select_db($database, $mssql);
    }
    function query($query)
    {
        if($type == "mysql")
        {
        mysql_query($query);
        }
        else if($type == "mysqli")
        {
        $mysqli->query($query);
        }
        else if($type == "mssql")
        {
        mssql_query($query);
        }
    }
}
?>

 

and

 

aanmelden(php)

<?php
include("config.php");
connectdatabase("mysql", "[ledensysteem]");   //example
if(!empty($_POST))
{
    if(!preg_match('/^[A-Za-z1-9-]{'.$Minimale_Gebruikersnaam_Karakters.',}$/', $_POST['gebruikersnaam']))
    {
    if(!isset($error)){ $error=1;}else{$error=$error+1;}
    echo "Je gebruikersnaam moet minimaal {$Minimale_Gebruikersnaam_Karakters} tekens bevaten en mag geen komma of andere onbedoelde tekens zijn<br>Toegestaan is  <br>A t/m Z <br>a t/m z <br>1 t/m 9 <br>en -";
    }
    else
    {
    echo "geldige gebruikersnaam(goedgekeurd)";
    }
    
    if(preg_match('/^[A-Za-z1-9-]{'.$Minimale_Wachtwoord_Karakters.',}$/', $_POST['wachtwoord']) && preg_match('/^[A-Za-z1-9-]{'.$Minimale_Wachtwoord_Karakters.',}$/', $_POST['herhaalwachtwoord']))
    {
        if($_POST['wachtwoord'] != $_POST['herhaalwachtwoord'])
        {
        if(!isset($error)){ $error=1;}else{$error=$error+1;}
        echo "niet hetzelfde wachtwoord";
        }
        /*else
        {
        echo "hetzelfde wachtwoord (goedgekeurd)";
        }*/
    }
    else
    {
    if(!isset($error)){ $error=1;}else{$error=$error+1;}
    echo "wachtwoord moet minimaal {$Minimale_Wachtwoord_Karakters} tekens bevatten!";
    }
    if(!preg_match("/^[A-Za-z1-9_.-]{1,}@[A-Za-z1-9-]{1,}\.[A-Za-z1-9]{2,3}$/", $_POST['email']))
    {
    if(!isset($error)){ $error=1;}else{$error=$error+1;}
    echo "onjuiste email";
    }
    else
    {
    echo "goedgekeurd!";
    }
    if(!isset($error)) // this problem is fixed yesterday on phpfreaks.com forum!
    {
    echo "goedgedaan geen errors!";
    query("SELECT username FROM phpfreaks WHERE password = private"); // example
    }
}
else
{
?>
<HTML>
<HEAD>
<TITLE>New Document</TITLE>
</HEAD>
<BODY>
<form method="post">
<input type="text" name="gebruikersnaam" value="gebruikersnaam" maxlength="20" size="20">
<input type="password" name="wachtwoord" value="wachtwoord" maxlength="20" size="20">
<input type="password" name="herhaalwachtwoord" value="herhaalwachtwoord" maxlength="20" size="20">
<input type="text" name="email" value="voorbeeld@domeinnaam.extensie" maxlength="50" size="20">
<input type="submit" name="login" value="inloggen">
</form>
</BODY>
</HTML>
<?php
}
?>

 

the problem is, is that i want to pass a variable between functions like $type  (the database type) between connectdatabase(); and query();

in the 'aanmelden.php' file is an example of how i use the function (on line 3 and 45  the ones with //example  comment)

 

thanks for reading. please help.

 

 

ps. config.php contains include("connect.php");

Link to comment
Share on other sites

In aanmelden.php, you could assign the type to a variable ($type for example). Then pass it to both functions.

 

<?php
...

$type = 'mysql';
connectdatabase($type, "[ledensysteem]");

...

query($type, "SELECT username FROM phpfreaks WHERE password = private");

...
?>

 

 

Of course you'll need to modify the query function to accept the second argument.

Link to comment
Share on other sites

I'd suggest you have your connectdatabase() function return an array:

 

array('type' = $type, $dbh =  //database handle );

 

The trick with resource variables is that you need to pass them by reference rather than value, so you'd need to return this array by reference.

 

$con = array();

$con['type'] = $type;

//.... make connection and assign

$con['dbh'] = mysql_connect($server, $username, $password);
//
// At the end return $con.

return &$con;

 

For your query function(s) specify that the $con array will be passed by reference as well.

 

function query(&$con) {

}

 

FWIW, this is a good argument for why PHP Oop can be helpful, and in particular having a singleton registry object is a helpful design pattern.

Link to comment
Share on other sites

I have edited my script.

Result:

<?php
function connectdatabase($type,$database)
{
    $server = "localhost";
    $username = "pjzlbina_test";
    $password = "test";
    global $querytype;
    $querytype = $type;
    if($type == "mysql")
    {
    $mysql = mysql_connect($server, $username, $password);
    mysql_select_db($database, $mysql);
    }
    else if($type == "mysqli")
    {
    $mysqli = new mysqli($server, $username, $password, $database);
    }
    else if($type == "mssql")
    {
    $mssql = mssql_connect($server, $username, $password);
    mssql_select_db($database, $mssql);
    }
    function query($query)
    {
        if($querytype == "mysql")
        {
        $test = mysql_query($query);
        echo "query{$report}";
        }
        else if($querytype == "mysqli")
        {
        $mysqli->query($query);
        }
        else if($querytype == "mssql")
        {
        mssql_query($query);
        }
    }
}
?>

and i have tested it with:

<?php
include("connect.php");
connectdatabase("mysql", "[ledensysteem]");
query("select email from [gebruikers] where id = 1");
?>

It doesn't return something?

 

@gizmola

 

can u translate this:

FWIW, this is a good argument for why PHP Oop can be helpful, and in particular having a singleton registry object is a helpful design pattern.

 

In an easy language and with no typo?

 

i don't understand the words:

FWIW, Singleton and design pattern.

Sorry my english is bad.

Link to comment
Share on other sites

FWIW = For What It's Worth

Design Pattern = A solution to a known problem

Singleton = A design pattern, problem: Only one instance of a certain object can exist at any given time.

Registry = A design pattern, problem: You need access to an object but you can't access it directly. For example you need the orders of a customer but you don't have the customer object (but through the Customer object you can retrieve the Orders). So, instead of calling/using a global variable you use a global access point to retrieve the instance.

 

$customer = Registry::get('Customer');

 

Internally this will do something like:

 

return self::getInstance()->offsetGet('Customer');

 

self::getInstance() returns the Singleton object.

 

If English is too hard for you, you can also PM me in Dutch.

Link to comment
Share on other sites

SOLVED

 

answer in programming language:

connect(php)

<?php
    $server = "localhost";
    $username = "pjzlbina_test";
    $password = "test";
function connectdatabase($type, $database)
{
    global $server,$username,$password,$querytype;
    if($type == "mysql")
    {
    $mysql = mysql_connect($server, $username, $password);
    mysql_select_db($database, $mysql);
    }
    $querytype = $type; //AT FUNCTION 1

    function query($query, $resulttype = "") //$resulttype = "" makes it optional to fill in.
    {
        global $queryresult,$querytype; // LOOK ITS NOW HERE  A FRIEND HELPED ME OUT 
        if($querytype == "mysql")
        {
            $query = mysql_query($query) or die('niet '.mysql_error().'');
        if($resulttype == "object")
            {
                $queryresult = mysql_fetch_object($query);
            }
        }
    }
}
?>

test(php)

<?php
include("connect.php");
connectdatabase("mysql", "pjzlbina_test");
query("SELECT * FROM `[gebruikers]` WHERE id = 1", "object");
echo "email is: {$queryresult->email}";
$load = microtime();
print (number_format($load,5));
?>

Link to comment
Share on other sites

function connectdatabase($server, $username, $password, $querytype, $type, $database)
{
   // function code
}

 

Also, your function definition for query() shouldn't be placed within an if-conditional.  Chances are, you want to invoke the function based on the if condition, not simply define the function.

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.