Akenatehm Posted February 28, 2009 Share Posted February 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/ Share on other sites More sharing options...
dt192 Posted February 28, 2009 Share Posted February 28, 2009 function userexists($in) { if(mysql_num_rows($in) > 0) { $error = '<font color="red"> User Already Exists </font>'; } } $checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'"); userexists($checkuser); Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773509 Share on other sites More sharing options...
Akenatehm Posted February 28, 2009 Author Share Posted February 28, 2009 Ok Sweet. I fixed that. But now I am getting this: MySQL Check Error with Duplicate entry 'Cody' for key 'username' Any idea how i can get rid of it and make it display the error I already have ? Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773514 Share on other sites More sharing options...
dt192 Posted March 1, 2009 Share Posted March 1, 2009 i think you have username set as a key which meeans its uniqe and your trying to add to rows to the database both with the username cody, but i could be wrong lol Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773518 Share on other sites More sharing options...
Akenatehm Posted March 1, 2009 Author Share Posted March 1, 2009 Ok, i've fixed those. Now I get this stupid thing: MySQL Check Error with Saying that there is no error and that it is just syaing it... anyone know how to get rid of this? Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773524 Share on other sites More sharing options...
trq Posted March 1, 2009 Share Posted March 1, 2009 You pass the variable $createresourceuser to mysqlcheck() but never seem to assign anything to this variable. Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773527 Share on other sites More sharing options...
Akenatehm Posted March 1, 2009 Author Share Posted March 1, 2009 $createresourceuser is a variable in the createresourcesuser() function in functions.php . Is it not possible to check a function with another? Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773531 Share on other sites More sharing options...
trq Posted March 1, 2009 Share Posted March 1, 2009 You really need to read up on the use of functions and variable scope. Variables created within functions only exist within that function. That is part of the point of functions, to encapsulate code. Code within them should not mess up the global namespace. Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773533 Share on other sites More sharing options...
Akenatehm Posted March 1, 2009 Author Share Posted March 1, 2009 I see. I've fixed that. Is there any way to stop a certain amount of code from being executed if something happens? Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773536 Share on other sites More sharing options...
trq Posted March 1, 2009 Share Posted March 1, 2009 I see. I've fixed that. Is there any way to stop a certain amount of code from being executed if something happens? I'm not sure what you meen. die will stop execution. Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773538 Share on other sites More sharing options...
Akenatehm Posted March 1, 2009 Author Share Posted March 1, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773542 Share on other sites More sharing options...
Akenatehm Posted March 1, 2009 Author Share Posted March 1, 2009 Does Anyone?? Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773551 Share on other sites More sharing options...
trq Posted March 1, 2009 Share Posted March 1, 2009 Its hard to tell which piece of code is throwing that error, but if your looking for last inserted players id take a look at mysql_insert_id. Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773555 Share on other sites More sharing options...
Akenatehm Posted March 1, 2009 Author Share Posted March 1, 2009 Ok. mysql_insert_id is working. But it is still showing that same error. Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773557 Share on other sites More sharing options...
trq Posted March 1, 2009 Share Posted March 1, 2009 You need to debug your code and find out which call to mysqlcheck() is causing the error. Quote Link to comment https://forums.phpfreaks.com/topic/147361-solved-functions-and-include/#findComment-773607 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.