Jump to content

[SOLVED] Functions and Include


Akenatehm

Recommended Posts

Hey Guys,

 

I thought I'd give creating my own functions a go. I thought I'd try and clean up my code so it doesn't look so messy so I put most of my common functions in a file called functions.php and using

include 'functions.php';

I would call on them as needed. But there seems to be a problem with MySQL resources and interchanging them between different functions.

 

I get the following error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /Library/WebServer/Documents/game/functions.php on line 13

On a registration page.

 

Here's functions.php:

<?php

function mysqlcheck($query)
{
if (!$query)
{
	die('MySQL Check Error with ' . mysql_error());
}	
}

function userexists()
{
if(mysql_num_rows($checkuser) > 0)
{
$error = '<font color="red"> User Already Exists </font>';
}
}

function activationkey()
{
$activationKey =  mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();	
}

function getplayerid($username)
{
$findplayerid = mysql_query("SELECT playerid FROM accounts WHERE username = '$username'");	
}

function createresourceuser($playerid)
{
$createresourcesuser = mysql_query("INSERT INTO resources (playerid,gold,wood,stone) VALUES('$playerid','200','200','200')");	
}
?>

 

and register.php:

<?php
session_start();

if (isset($_POST['submit']))
{
if($_POST['password'] == $_POST['confirmpassword'])
{
		include 'connect.php';
		include 'functions.php';
		connect();
		$username = $_POST['username'];

		$checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'");

		mysqlcheck("$checkuser");
		userexists();
		$activationkey = activationkey();	

		$username = $_POST['username'];
		$password = $_POST['password'];
		$email = $_POST['email'];
		$password = md5($password);

		$adduser = mysql_query("INSERT INTO accounts (username, password, email, activationkey, last_online) VALUES ('$username' , '$password' , '$email', '$activationkey', '0')");

		mysqlcheck($adduser);
		getplayerid($username);					
		mysqlcheck($findplayerid);									

		$transferplayerid = mysql_fetch_assoc($findplayerid);
		$playerid = $transferplayerid['playerid'];	
		createresourceuser($playerid);
		mysqlcheck($createresourceuser);

		$result = '<font color="green">User Added Succesfully </font>';
}

else
{				
$result = '<font color="red"> Passwords Do Not Match </font>';		
}
}
?>
<html>
<head>
<title>Registration - Fall of The Empire</title>
<link rel="stylesheet" href="styles.css" type="text/css"/>
</head>
<body bgcolor="272d31">
	<div align="center" class="header">
		<img src="images/logo.png" width="473" height="227" alt="Logo">
</div>
<br>
<center>
<div class="main">
<div class="navbar">
	<img src="images/nav_top.png" width="232" height="46" alt="Nav Top"><br>
	<a href="index.php"><img src="images/nav_home.png" width="232" height="33" alt="Nav Home" border="0"></a><br>
	<a href="news.php"><img src="images/nav_news.png" width="232" height="32" alt="Nav News" border="0"></a><br>
	<a href="about.php"><img src="images/nav_about.png" width="232" height="23" alt="Nav About" border="0"></a><br>
	<a href="login.php"><img src="images/nav_login.png" width="232" height="32" alt="Nav Login" border="0"></a><br>
	<a href="register.php"><img src="images/nav_register.png" width="232" height="25" alt="Nav Register" border="0"></a><br>
	<a href="contactus.php"><img src="images/nav_bottom.png" width="232" height="179" alt="Nav Bottom" border="0"><br></a>
	</div>	

	<div class="cbtop" align="center">
	<img src="images/register.png" width="171" height="25" alt="Register" class="welcome" align="right">	
	</div>
	<br><br>
	<div class="middle"><hr><br>
	<br>
	<table width="300px" align="right">
		<form action="<?php echo $PHP_SELF; ?>" method="POST">
			<tr>
				<td>
					<font color="white">
						Username:
					</font>
				</td>

				<td>
					<input type="text" name="username">
				</td>
			</tr>

			<tr>
				<td>
					<font color="white">
						Email
					</font>			
				</td>

				<td>
					<input type="text" name="email">
				</td>
			</tr>

			<tr>
				<td>
					<font color="white">
						Password:
					</font>
				</td>

				<td>
					<input type="password" name="password">
				</td>
			</tr>

			<tr>
				<td>
					<font color="white">
						Confirm Password:
					</font>
				</td>

				<td>
					<input type="password" name="confirmpassword">
				</td>
			</tr>

			<tr>
				<td>
					<br>
				</td>
				<td>
				</td>
			<tr>
				<td>
				</td>
		<td align="right" style="padding-right:8px">
		<input type="submit" name="submit" value="Register"align="right">
		</td>
		</tr>
		</form>
	</table>
	<p><?php echo $result; ?></p>
	<p class="heading" align="right"><br><br><br><br><br><br><br><br><br><br></p>	
	<div class="cbbottom" align="center">
<img src="images/bottom.png" width="486" height="25" alt="Bottom" align="center">
	</div>
	</div>		
	<br>
	</div>
	</div>
	</center>
</body>
</html>

 

Any help, would be greatly appreciated.

Link to comment
Share on other sites

I used. and If ...Else function.

 

Here's my code:

functions.php

<?php

function mysqlcheck($query)
{
if (!$query)
{
	die('MySQL Check Error with ' . mysql_error());
}	
}

?>

register.php

<?php
session_start();

if (isset($_POST['submit']))
{
if($_POST['password'] == $_POST['confirmpassword'])
{
		include 'connect.php';
		include 'functions.php';
		connect();
		$username = $_POST['username'];

		$checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'");

		mysqlcheck($checkuser);
		if(mysql_num_rows($checkuser) > 0)
		{
		$error = '<font color="red"> User Already Exists </font>';
		}
		else
		{

		$activationkey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();	

		$username = $_POST['username'];
		$password = $_POST['password'];
		$email = $_POST['email'];
		$password = md5($password);

		$adduser = mysql_query("INSERT INTO accounts (username, password, email, activationkey, last_online) VALUES ('$username' , '$password' , '$email', '$activationkey', '0')");

		mysqlcheck($adduser);

		$findplayerid = mysql_query("SELECT playerid FROM accounts WHERE username = '$username'");

		mysqlcheck($findplayerid);									

		$transferplayerid = mysql_fetch_assoc($findplayerid);
		$playerid = $transferplayerid['playerid'];	

		$createresourcesuser = mysql_query("INSERT INTO resources (playerid,gold,wood,stone) VALUES('$playerid','200','200','200')");	

		mysqlcheck($createresourceuser);

		$result = '<font color="green">User Added Succesfully </font>';
}
}
else
{				
$result = '<font color="red"> Passwords Do Not Match </font>';		
}
}
?>

<html>
<head>
<title>Registration - Fall of The Empire</title>

<?php 
include 'page_elements.php';
abovenav();
navbar();
?> 
<div class="cbtop" align="center">
	<img src="images/register.png" width="171" height="25" alt="Register" class="welcome" align="right">	
	</div>
	<br><br>
	<div class="middle"><hr><br>
	<br>
	<table width="300px" align="right">
		<form action="<?php echo "$PHP_SELF";?>" method="POST">
			<tr>
				<td>
					<font color="white">
						Username:
					</font>
				</td>

				<td>
					<input type="text" name="username">
				</td>
			</tr>

			<tr>
				<td>
					<font color="white">
						Email
					</font>			
				</td>

				<td>
					<input type="text" name="email">
				</td>
			</tr>

			<tr>
				<td>
					<font color="white">
						Password:
					</font>
				</td>

				<td>
					<input type="password" name="password">
				</td>
			</tr>

			<tr>
				<td>
					<font color="white">
						Confirm Password:
					</font>
				</td>

				<td>
					<input type="password" name="confirmpassword">
				</td>
			</tr>

			<tr>
				<td>
					<br>
				</td>
				<td>
				</td>
			<tr>
				<td>
				</td>
		<td align="right" style="padding-right:8px">
		<input type="submit" name="submit" value="Register"align="right">
		</td>
		</tr>
		<tr>
			<td>
				<br>
			</td>
			<td>
			</td>
		<tr>
			<td>
			</td>
			<td><?php echo $result; ?>
			</td>
			</tr>
		</form>
	</table>
<?php
footer();
?>


</body>
</html>

 

and it is still showing up with:

MySQL Check Error with

 

Do you have any idea why its doing that?

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.