Hey, i'm wanting to learn more about functions. Is there a way this code below can be turned into a function? I want to have a functions page instead of having required pages pulled in for certain areas. Right now it's setup so the index page requires this(points.php) file which contains: <?php session_start(); require('configure.php'); //---Variables---// $connection = mysql_connect($host,$dbuser,$dbpass) or die('MySQl Connection Error:' . mysql_error()); mysql_select_db($dbname,$connection) or die('MySQL Error: Cannot select table'); //---END Variables---// //---If cannot connect---// if (!$connection) { die("Could not connect: " . mysql_error()); } else { //---Everything is OK, Run---// $username = $_SESSION['username']; $sql = mysql_query("SELECT points FROM users WHERE username=('$username')") or die(mysql_error()); // Mysql fetch row results $row = mysql_fetch_assoc($sql); echo ($row['points']); mysql_close($connection); } ?> PHP:
Try this; <?php function your_function($host,$dbuser,$dbpass,$dbname){ session_start(); require('configure.php'); //---Variables---// $connection = mysql_connect($host,$dbuser,$dbpass) or die('MySQl Connection Error:' . mysql_error()); mysql_select_db($dbname,$connection) or die('MySQL Error: Cannot select table'); //---END Variables---// //---If cannot connect---// if (!$connection) { die("Could not connect: " . mysql_error()); } else { //---Everything is OK, Run---// $username = $_SESSION['username']; $sql = mysql_query("SELECT points FROM users WHERE username=('$username')") or die(mysql_error()); // Mysql fetch row results $row = mysql_fetch_assoc($sql); echo ($row['points']); mysql_close($connection); } } ?> PHP:
Thank you, removing: $host,$dbuser,$dbpass,$dbname from function your_function($host,$dbuser,$dbpass,$dbname){ worked. LOL thanks
You must have those variables saved in your session or have register_globals turned on. Sometimes if $_SESSION['host'] is set then the default value for $host is set to this variable.
no I don't. I have $host,$dbpass,etc variables set in configure.php which is pulled in with require('configure.php'); in the function.