3raser Posted March 29, 2011 Share Posted March 29, 2011 Whenever a valid ID is entered, my page stops loading and when it finally does load, it gives a lot of MySQL connection errors. What is wrong with my function code? <?php include_once("includes/config.php"); if(!$_GET['id'] && $_POST['id']) { $id = mysql_real_escape_string($_POST['id']); } elseif($_GET['id'] && !$_POST['id']) { $id = mysql_real_escape_string($_GET['id']); } else { } if(!$id) { $content = "Sorry, you have not selected a skin to view."; } else { $extract_information = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1"); function displayBody($id) { mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'"); $content = (displayBody($id)) ? $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." - <a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/> <img src='skins/". $extract['id'] .".png' width='500' height='300'>" : NULL; return true; } if(mysql_num_rows($extract_information) == 0) { $content = "Sorry, no skin exists with this ID."; } else { $extract = mysql_fetch_assoc($extract_information); if($_GET['download']) { $does_exist_download = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1"); if(mysql_num_rows($does_exist_download) == 0) { $content = "No skin exists with this ID, so you may not download."; } else { mysql_query("UPDATE skins SET downloads = downloads + 1 WHERE id = '". mysql_real_escape_string($_GET['download']) ."'"); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename=skins/".$extract['id'].".png"); header( "Content-Description: File Transfer"); @readfile($file); } } elseif(!$extract['password']) { displayBody($id); } elseif(!$_POST['password']) { $content = "<br/><br/><div id='header'>Password</div> <center><form action='view.php' method='POST'> <input type='hidden' name='id' value='". $id ."'>Password: <input type='password' name='password' maxlength='6'> <input type='submit' value='View'></form></center>"; } else { if($_POST['password'] != $extract['password']) { $content = "You have entered in an incorrect password. <a href='view.php?id=". $id ."'>Try Again</a> or <a href='index.php'>Home</a>."; } else { displayBody($id); } } } } ?> <html> <head> <title><?php $title; ?></title> <link rel="stylesheet" type="text/css" href="theme/style.css" /> </head> <body> <div id="header"> MCSkins </div> <?php echo $content; ?> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/ Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 Have you got an open connection before calling this code? mysql_real_escape_string() requires a connection. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193946 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 Yes, I do: include_once("includes/config.php"); Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193971 Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 You might want to tell us what errors your getting then. I don't much like guessing games. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193975 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 You might want to tell us what errors your getting then. I don't much like guessing games. If the page would load I'd show you. :/ http://stonedknights.net46.net/view.php?id=14 Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193979 Share on other sites More sharing options...
jcbones Posted March 30, 2011 Share Posted March 30, 2011 You might want to tell us what errors your getting then. I don't much like guessing games. If the page would load I'd show you. :/ http://stonedknights.net46.net/view.php?id=14 it gives a lot of MySQL connection errors. Ummm, ??? Anyway, I told you in another thread that your variables are out of scope in the function displayBody(), I also told you how to fix it. You should really read up on variable scope. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193982 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 You might want to tell us what errors your getting then. I don't much like guessing games. If the page would load I'd show you. :/ http://stonedknights.net46.net/view.php?id=14 it gives a lot of MySQL connection errors. Ummm, ??? Anyway, I told you in another thread that your variables are out of scope in the function displayBody(), I also told you how to fix it. You should really read up on variable scope. Yes, and I'm using the code you suggested for me to use. :/ And reading up now. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193987 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 Ok, it seems changing my code to this: function displayBody($id) { global $content; mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'"); $content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." - <a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/> <img src='skins/". $extract['id'] .".png' width='500' height='300'>"; } Gave me this...weird error: User 'a3473343_data' has exceeded the 'max_questions' resource (current value: 100000) Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193990 Share on other sites More sharing options...
jcbones Posted March 30, 2011 Share Posted March 30, 2011 Sorry, I focused on the return part, and left out the other parts. function displayBody($id) { $result = mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'"); return true; } Then: $content = (displayBody($id)) ? $extract['title']. ", by ". $extract['username'] ."." : NULL; You ran out of scope with the function, you must put ID into the function scope. As well, the $extract array is out of scope with the function. Since there is no reason to send an argument to the function just to return it, handle the $extracts outside of the function, based on how the function acts. So, we return the function as true, to enable us to decide which string should display. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193993 Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 Justin, don't use globals in functions. They break the entire point of encapsulation. I didn't see the rest of your code in your post (didn't scroll down for whatever reason) but yeah, there is quite allot of issues within. I suggest your take a good look at the link that jcbones provided on scope and maybe start again. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1193995 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 Justin, don't use globals in functions. They break the entire point of encapsulation. I didn't see the rest of your code in your post (didn't scroll down for whatever reason) but yeah, there is quite allot of issues within. I suggest your take a good look at the link that jcbones provided on scope and maybe start again. Er....global is pretty much based from the link he gave me, and it's really the only thing it talks about scopes. :? Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194012 Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 Functions accept arguments and return values, you use these to pass data in and get data out of functions. Using globals breaks a functions encapsulation and is considered poor design. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194014 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 Functions accept arguments and return values, you use these to pass data in and get data out of functions. Using globals breaks a functions encapsulation and is considered poor design. Is their a documentation on that then? I really don't see why global is that bad though, it just lets other section of the code access the data and also makes changes to $content outside the function, which is exactly what I need it do do. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194016 Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 Is their a documentation on that then? Within the functions section of the manual. I really don't see why global is that bad though, it just lets other section of the code access the data and also makes changes to $content outside the function, which is exactly what I need it do do. Most newcomers don't. That's why it's such a trap. The bigger picture is that eventually you will be working with someone else's code. Now, lets say they have a function documented like so.... void foo ( string $name ) This says that the function foo excepts 1 argument, a string to be used within the function as $name. Now, if this function also relies upon a global $content you have no way of knowing about it and you cannot use the function without first defining $content somewhere. Functions accept arguments so that they are self documenting, sure, you could go and look at the code for the foo() function but you shouldn't have to. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194019 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 Is their a documentation on that then? Within the functions section of the manual. I really don't see why global is that bad though, it just lets other section of the code access the data and also makes changes to $content outside the function, which is exactly what I need it do do. Most newcomers don't. That's why it's such a trap. The bigger picture is that eventually you will be working with someone else's code. Now, lets say they have a function documented like so.... void foo ( string $name ) This says that the function foo excepts 1 argument, a string to be used within the function as $name. Now, if this function also relies upon a global $content you have no way of knowing about it and you cannot use the function without first defining $content somewhere. Functions accept arguments so that they are self documenting, sure, you could go and look at the code for the foo() function but you shouldn't have to. So the code I'm looking for is: function displayBody($id) { mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'"); $content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." - <a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/> <img src='skins/". $extract['id'] .".png' width='500' height='300'>"; return $content; } Sorry if I'm getting annoying, I just like to make sure I do things right before I start putting it into action. And right now I don't have the option to test, seeing as my website is under review. I hate my host. -.- EDIT: After this post is solved, can an admin/moderator delete all my posts/threads? My 500 posts are usually just questions, and I feel thats a bit....crappy. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194034 Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 $extract is not defined within that function. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194037 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 $extract is not defined within that function. So, would it be included with $id in the parameter? Also, can you reset my post count and delete all my threads and posts besides this one, or just delete everything after this is solved? Have 500 questions as postcount makes me feel like the 500 is....well...not deserved. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194039 Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 So, would it be included with $id in the parameter? Yes, if you want to use it within your function you will need to pass it into the function. Also, can you reset my post count and delete all my threads and posts besides this one, or just delete everything after this is solved? Have 500 questions as postcount makes me feel like the 500 is....well...not deserved. As you have just noted, post counts are not an indication of knowledge, in fact, there not really an indication of anything except how many times you've hit the submit button. We do not tamper ewith people's counts however. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194040 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 Thanks, this is now solved. What ever happened to the solve button? Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194046 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 Erhm, changed my code too this, and now the page will take 30 seconds to load. It seems the function may be repeating itself over and over and over, because sometimes the views row jumps to 20 thousand. <?php include_once("includes/config.php"); if(!$_GET['id'] && $_POST['id']) { $id = mysql_real_escape_string($_POST['id']); } elseif($_GET['id'] && !$_POST['id']) { $id = mysql_real_escape_string($_GET['id']); } else { } if(!$id) { $content = "Sorry, you have not selected a skin to view."; } else { $extract_information = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1") or die(mysql_error()); function displayBody($id,$extract) { mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'"); $content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." - <a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/> <img src='skins/". $extract['id'] .".png' width='500' height='300'>"; return $content; } if(mysql_num_rows($extract_information) == 0) { $content = "Sorry, no skin exists with this ID."; } else { $extract = mysql_fetch_assoc($extract_information); if($_GET['download']) { $does_exist_download = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1"); if(mysql_num_rows($does_exist_download) == 0) { $content = "No skin exists with this ID, so you may not download."; } else { mysql_query("UPDATE skins SET downloads = downloads + 1 WHERE id = '". mysql_real_escape_string($_GET['download']) ."'"); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename=skins/".$extract['id'].".png"); header( "Content-Description: File Transfer"); @readfile($file); } } elseif(!$extract['password']) { displayBody($id); } elseif(!$_POST['password']) { $content = "<br/><br/><div id='header'>Password</div> <center><form action='view.php' method='POST'> <input type='hidden' name='id' value='". $id ."'>Password: <input type='password' name='password' maxlength='6'> <input type='submit' value='View'></form></center>"; } else { if($_POST['password'] != $extract['password']) { $content = "You have entered in an incorrect password. <a href='view.php?id=". $id ."'>Try Again</a> or <a href='index.php'>Home</a>."; } else { displayBody($id); } } } } ?> <html> <head> <title><?php $title; ?></title> <link rel="stylesheet" type="text/css" href="theme/style.css" /> </head> <body> <div id="header"> MCSkins </div> <?php echo $content; ?> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194558 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 PLEASE DELETE THIS TOPIC. NEW ONE STARTED. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194603 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 Code: <?php include_once("includes/config.php"); if(!$_GET['id'] && $_POST['id']) { $id = mysql_real_escape_string($_POST['id']); } elseif($_GET['id'] && !$_POST['id']) { $id = mysql_real_escape_string($_GET['id']); } else { } if(!$id) { $content = "Sorry, you have not selected a skin to view."; } else { $extract_information = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1") or die(mysql_error()); function displayBody($id,$extract) { mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'"); $content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." - <a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/> <img src='skins/". $extract['id'] .".png' width='500' height='300'>"; return $content; } if(mysql_num_rows($extract_information) == 0) { $content = "Sorry, no skin exists with this ID."; } else { $extract = mysql_fetch_assoc($extract_information); if($_GET['download']) { $does_exist_download = mysql_query("SELECT title,username,id,password,description FROM skins WHERE id = '$id' LIMIT 1"); if(mysql_num_rows($does_exist_download) == 0) { $content = "No skin exists with this ID, so you may not download."; } else { mysql_query("UPDATE skins SET downloads = downloads + 1 WHERE id = '". mysql_real_escape_string($_GET['download']) ."'"); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename=skins/".$extract['id'].".png"); header( "Content-Description: File Transfer"); @readfile($file); } } elseif(!$extract['password']) { displayBody($id); } elseif(!$_POST['password']) { $content = "<br/><br/><div id='header'>Password</div> <center><form action='view.php' method='POST'> <input type='hidden' name='id' value='". $id ."'>Password: <input type='password' name='password' maxlength='6'> <input type='submit' value='View'></form></center>"; } else { if($_POST['password'] != $extract['password']) { $content = "You have entered in an incorrect password. <a href='view.php?id=". $id ."'>Try Again</a> or <a href='index.php'>Home</a>."; } else { displayBody($id); } } } } ?> <html> <head> <title><?php $title; ?></title> <link rel="stylesheet" type="text/css" href="theme/style.css" /> </head> <body> <div id="header"> MCSkins </div> <?php echo $content; ?> </center> </body> </html> The function: function displayBody($id,$extract) { mysql_query("UPDATE skins SET views = views + 1 WHERE id = '$id'"); $content = $extract['title']. ", by ". $extract['username'] .".<br/><br/>Description: ". $extract['description'] ." - <a href='view.php?download=". $extract['id'] ."'>Download</a><br/><br/> <img src='skins/". $extract['id'] .".png' width='500' height='300'>"; return $content; } Error: Warning: Missing argument 2 for displayBody(), called in /home/a3473343/public_html/view.php on line 72 and defined in /home/a3473343/public_html/view.php on line 25 Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194646 Share on other sites More sharing options...
Zane Posted March 30, 2011 Share Posted March 30, 2011 "Missing argument 2" is shorthand for "You are missing the second argument/parameter" displayBody($id); See, there is only one argument Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194651 Share on other sites More sharing options...
3raser Posted March 30, 2011 Author Share Posted March 30, 2011 "Missing argument 2" is shorthand for "You are missing the second argument/parameter" displayBody($id); See, there is only one argument Well, I managed to get rid of the error. But why is it when I view my page, no results are echoed?... Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194654 Share on other sites More sharing options...
Zane Posted March 30, 2011 Share Posted March 30, 2011 because your displayBody function doesn't echo anything... it only returns. Quote Link to comment https://forums.phpfreaks.com/topic/232114-messed-up-function/#findComment-1194661 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.