Need to turn this into a function

Discussion in 'PHP' started by sandstorm140, Jun 25, 2008.

  1. #1
    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:
     
    sandstorm140, Jun 25, 2008 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    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:
     
    Weirfire, Jun 25, 2008 IP
  3. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you, removing: $host,$dbuser,$dbpass,$dbname from function your_function($host,$dbuser,$dbpass,$dbname){ worked. LOL :) thanks
     
    sandstorm140, Jun 25, 2008 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    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.
     
    Weirfire, Jun 25, 2008 IP
  5. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    sandstorm140, Jun 25, 2008 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    That explains it then :)
     
    Weirfire, Jun 25, 2008 IP