mrbean Posted April 16, 2011 Share Posted April 16, 2011 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"); Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/ Share on other sites More sharing options...
cyberRobot Posted April 16, 2011 Share Posted April 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202231 Share on other sites More sharing options...
mrbean Posted April 16, 2011 Author Share Posted April 16, 2011 can i do this: function a($type, $databasename) { $querytype = $type; function b($query) { echo $querytype; } } a(mysql, [ledensysteem]); b(select a from b where c = c); in example Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202310 Share on other sites More sharing options...
cyberRobot Posted April 16, 2011 Share Posted April 16, 2011 You could always try it out. I would imagine that it's not going to work; but I'm not positive on that. You may want to review the PHP manual for Variable Scope: http://php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202316 Share on other sites More sharing options...
mrbean Posted April 16, 2011 Author Share Posted April 16, 2011 so $querytype must be: global $querytype ? Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202399 Share on other sites More sharing options...
gizmola Posted April 16, 2011 Share Posted April 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202403 Share on other sites More sharing options...
mrbean Posted April 16, 2011 Author Share Posted April 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202426 Share on other sites More sharing options...
ignace Posted April 17, 2011 Share Posted April 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202521 Share on other sites More sharing options...
mrbean Posted April 17, 2011 Author Share Posted April 17, 2011 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202728 Share on other sites More sharing options...
KevinM1 Posted April 17, 2011 Share Posted April 17, 2011 Don't use 'global' to pass parameters into your functions. Pass them through the argument list. That's why it's there. Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202730 Share on other sites More sharing options...
mrbean Posted April 18, 2011 Author Share Posted April 18, 2011 can u please give me an example on how to use it? Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202790 Share on other sites More sharing options...
KevinM1 Posted April 18, 2011 Share Posted April 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1202920 Share on other sites More sharing options...
mrbean Posted April 18, 2011 Author Share Posted April 18, 2011 I dont want to re-type an variable if i use it on a function before. And what else can i use in place of if-condition? ternary operator? Quote Link to comment https://forums.phpfreaks.com/topic/233869-passing-variables-between-functions/#findComment-1203085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.