Can't connect to database inside function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Can't connect to database inside function

Post by davidprogramer »

It connects fine on the load. But when I click submit, it fails. Any way to fix this???

Code: Select all

/****************************************
modules/League/includes/league_functions.php
****************************************/
function connection($sql_db, $sql_username, $sql_pw){
	$hostname = "localhost";
	$database = "$sql_db";
	$username = "$sql_username";
	$password = "$sql_pw";
	$dbh = mysql_connect($hostname, $username, $password) 
		or die("Unable to connect to MySQL");

	mysql_select_db("$database",$dbh) 
		or die("Error: Could not connect to database: $database ");
}

Code: Select all

<?
/****************************************
create_clan.php
****************************************/
<?
include("modules/League/includes/league_functions.php");

$user_id = $cookie[0];
$player_id = $_GET[pid];
$current_ladder = $_GET[lid];

$ladders = sql_query("SELECT ladder_id, ladder_name, ladder_directory, sql_db, sql_username, sql_pw, sql_prefix, pri_color, sec_color
						FROM ".$prefix."_league_ladders WHERE ladder_id = $current_ladder");
list($ladder_id, $ladder_name, $ladder_directory, $sql_db, $sql_username, $sql_pw, $sql_prefix, $pri_color, $sec_color) = sql_fetch_row($ladders);
		
connection($sql_db, $sql_username, $sql_pw);

function show_form($clan_name="",$clan_tag="", $clan_email="", $clan_avatar="", $clan_website="", $clan_aim="", $clan_msnm="",
		$clan_icq="", $clan_xfire="", $clan_yim="", $clan_challenge="", $clan_viewemail="") {
		
?>

<form action="modules.php?name=League&file=create_clan" method="post">
	Clan Name:
		<input type=text name=clan_name value="<?echo $clan_name?>"><br>
	Clan Tag:
		<input type=text name=clan_tag value="<?echo $clan_tag?>"><br>
	Clan Email:
		<input type=text name=clan_email value="<?echo $clan_email?>"><br>
	AOL:
		<input type=text name=clan_aim value="<?echo $clan_aim?>"><br>
	MSN:
		<input type=text name=clan_msnm value="<?echo $clan_msnm?>"><br>
	ICQ:
		<input type=text name=clan_icq value="<?echo $clan_icq?>"><br>
	YAHOO:
		<input type=text name=clan_yim value="<?echo $clan_yim?>"><br>
	X-FIRE:
		<input type=text name=clan_xfire value="<?echo $clan_xfire?>"><br>
	Recieve Challenges:
		<input type=radio name=clan_challenge value=1 <?php if($_POST['clan_challenge'] == 1); ?>>Yes &nbsp; 
		<input type=radio name=clan_challenge value=0<?php if($_POST['clan_challenge'] == 0); ?> >No 
	Other Clans View Email?:<br> 
		<input type=radio name=clan_viewemail value=1 <?php if($_POST['clan_viewemail'] == 1); ?>>Yes &nbsp; 
		<input type=radio name=clan_viewemail value=0<?php if($_POST['clan_viewemail'] == 0); ?> >No 

<input type=submit>
</form>

<? } 

if(!isset($clan_name)) {
        show_form();
}
else {
   if(empty($clan_name) or empty($clan_tag) or empty($clan_email)){
        echo "Error 01: You did not fill in all required fields, please try again<p>"; 
        show_form($clan_name,$clan_tag, $clan_email, $clan_avatar, $clan_website, $clan_aim, $clan_msnm,
		$clan_icq, $clan_xfire, $clan_yim, $clan_challenge, $clan_viewemail);
	}
	else{
	
	   $sql_clans = 'SELECT clan_id, clan_tag AS clantag, clan_name AS clanname FROM `'.$sql_prefix.'_clans`'; 
		
		$result_clans = mysql_query($sql_clans) or die('SQL ['.$sql_clans.'] caused: '. mysql_error()); 
	

		$clans = array(); 
 

		while ($row = mysql_fetch_assoc($result_clans)) { 

			$clans[] = $row['clan_id']; 
		} 
		$num_clans = count($clans);
	

		for ($x = 0; $x <=  $num_clans; $x ++) { 
			if ($clan_name == $clanname){
				echo "Error 02: Clan Name already exists. Please change it.";
				die();
			}else if ($clan_tag == $clantag){
				echo "Error 03: Clan Tag already exists. Please change it.";
				die();
			}else

			$create_clan = mysql_query("INSERT INTO `".sql_prefix."_clans` ( //fields )
			
			VALUES ( //values);");
			echo "Clan was successfully created!";
			}	
		}
	}
?>
Last edited by davidprogramer on Mon Dec 05, 2005 1:11 am, edited 2 times in total.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

did you open a connection on modules.php? which is where the form submits to.
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Post by davidprogramer »

hmmm...no. But it goes to "League" then index.php inside "League". This is phpNuke.

Though, you did open up an idea. So I took the bottom code (the code that does the error checking, inserting, etc.) and put it in a file called create_clan_finish.php

Then I changed the following line:

Code: Select all

<form action="modules.php?name=League&file=create_clan&lid=$current_ladder&pid=$player_id" method="post">
to

Code: Select all

<form action="modules.php?name=League&file=create_clan_finish&lid=$current_ladder&pid=$player_id" method="post">
This still results in a failure to connect to the database. Any ideas on how to finish this form up once and for all?

I can tell you right now it's the way I set the form up to work (using functions). How else could I have set this form up? I updated the code above to show the most recent version of the script.

Ok. I am VERY CLOSE now. The only problem now is the variables aren't working. (Before they weren't even showing as variables).
Can a function get global variables from the phpscript it is in??? I don't think so. If not, how do I get the ladder id and player id to the function?


Scratch that. I got it working. But the only problem now is my query. Is something wrong with it??? Here is the code~

Code: Select all

if(!isset($clan_name)) {
        show_form($current_ladder, $player_id);
}
else{
   if(empty($clan_name) or empty($clan_tag) or empty($clan_email)){
        echo "Error 01: You did not fill in all required fields, please try again<p>"; 
        show_form($current_ladder, $player_id, $clan_name,$clan_tag, $clan_email, $clan_avatar, $clan_website, $clan_aim, $clan_msnm,
		$clan_icq, $clan_xfire, $clan_yim, $clan_challenge, $clan_viewemail);
	}
	else{
	   $sql_clans = 'SELECT clan_id, clan_tag AS clantag, clan_name AS clanname FROM `'.$sql_prefix.'_clans`'; 
		//perform the update query 
		$create_clan = mysql_query("INSERT INTO `".sql_prefix."_clans` ( `clan_id` , `clan_rank` , `clan_name` , `clan_tag` , `clan_leader` ,
		`recieve_challenges` , `clan_wins` , `clan_losses` , `clan_points` , `clan_streak` , `clan_honor` , `clan_regdate` , `clan_avatar` ,
		`clan_email` , `clan_website` , `clan_aim` , `clan_msnm` , `clan_icq` , `clan_yim` , `clan_xfire` , `clan_status` ,
		`clan_disputes` , `clan_banned` , `clan_viewemail` , `clan_rank1` , `clan_rank2` , `clan_rank3` , `clan_rank4` ,
		`clan_rank5` , `career_wins` , `career_losses` , `career_points` , `career_games` , `career_hi_streak` ,
		`career_lo_streak` , `career_disputes` )
		
		VALUES ( 'DEFAULT', 'DEFAULT', '$clan_name', '$clan_tag', '$player_id', '$clan_challenge', 'DEFAULT', 'DEFAULT', DEFAULT ,
		'DEFAULT', 'DEFAULT', 'DEFAULT', 'DEFAULT', '$clan_email', '$clan_website', '$clan_aim', '$clan_msnm', '$clan_icq', '$clan_yim', '$clan_xfire',
		'DEFAULT', 'DEFAULT', 'DEFAULT', '$clan_viewemail', 'DEFAULT', 'DEFAULT', 'DEFAULT', 'DEFAULT', 'DEFAULT', 'DEFAULT', 'DEFAULT', 'DEFAULT',
		'DEFAULT', 'DEFAULT', 'DEFAULT', 'DEFAULT');");
		$result_clans = mysql_query($sql_clans) or die('SQL ['.$sql_clans.'] caused: '. mysql_error()); 

		echo "Clan was successfully created!";
	}
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Can't connect to database inside function

Post by jmut »

davidprogramer wrote:It connects fine on the load. But when I click submit, it fails. Any way to fix this???

Code: Select all

/****************************************
modules/League/includes/league_functions.php
****************************************/
function connection($sql_db, $sql_username, $sql_pw){
	$hostname = "localhost";
	$database = "$sql_db";
	$username = "$sql_username";
	$password = "$sql_pw";
	$dbh = mysql_connect($hostname, $username, $password) 
		or die("Unable to connect to MySQL");

	mysql_select_db("$database",$dbh) 
		or die("Error: Could not connect to database: $database ");
}
I am a little confused with this function.
It does not return anything. Nor it assignes $dbh to the global scope.
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Post by davidprogramer »

It just connects to a database. The database is dynamic. It can be 1 of 3 to 10 databases. Its hard to explain. It does work though.


edit...Ok this is embarassing -_-

Code: Select all

$join_league = mysql_query("INSERT INTO ".$sql_prefix."_clans ( `
There was a " ` right after _clans. Works perfect now!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

davidprogramer wrote:It just connects to a database. The database is dynamic. It can be 1 of 3 to 10 databases. Its hard to explain. It does work though.


edit...Ok this is embarassing -_-

Code: Select all

$join_league = mysql_query("INSERT INTO ".$sql_prefix."_clans ( `
There was a " ` right after _clans. Works perfect now!
It's a really common... and hard to spot error, especially with a fair few editors ;)
Post Reply