katarra Posted March 4, 2010 Share Posted March 4, 2010 I'm having a problem with the result coming up as 0 so it just says I have "no energy left", which isn't the case. When I put a print $data into the mix, it comes up as a blank screen, so it's obviously not pulling anything. Here's the part of the code that isn't working right. Any help would be AWESOME! Thanks! <?php function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); $result = mysql_query("SELECT * FROM users WHERE username = '$username'"); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,$var)); mysql_close($dbh); $data = str_replace("'","'",$data); return $data;} $data = $user; $expgain = 1; $expgain2 = 2; if ($user->energy <= 0) { echo "You have no energy left!" ; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/ Share on other sites More sharing options...
jskywalker Posted March 4, 2010 Share Posted March 4, 2010 change this: $result = mysql_query("SELECT * FROM users WHERE username = '$username'"); to: $result = mysql_query("SELECT * FROM users WHERE username = '".$username."'"); Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1021669 Share on other sites More sharing options...
schilly Posted March 4, 2010 Share Posted March 4, 2010 that wont make a difference. where is $var defined? if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,$var)); Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1021670 Share on other sites More sharing options...
katarra Posted March 4, 2010 Author Share Posted March 4, 2010 I guess I haven't done that yet. Could you give me a pointer as to what I would need to set it as? I wasn't aware I needed to (I'm still new to this, obviously). Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1021674 Share on other sites More sharing options...
schilly Posted March 4, 2010 Share Posted March 4, 2010 well $var is the field that you want to get. so either do this: if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,'field_name')); or just select the one field in your query and do this: $result = mysql_query("SELECT field_name FROM users WHERE username = '$username'"); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0)); Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1021689 Share on other sites More sharing options...
katarra Posted March 4, 2010 Author Share Posted March 4, 2010 What I'm wanting to do is have it find the field "energy" on a specific row (username) and then have it determine if "energy" > 0, and if so, let them do fishing. For field_name, would I just put in "energy" then? Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1021690 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 Anybody? *laughs* Trying to jump on this to get it all done. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022033 Share on other sites More sharing options...
schilly Posted March 5, 2010 Share Posted March 5, 2010 What I'm wanting to do is have it find the field "energy" on a specific row (username) and then have it determine if "energy" > 0, and if so, let them do fishing. For field_name, would I just put in "energy" then? if thats what the field is named in the db then yes. Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022034 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 Hmm... tried both ways listed, and neither one worked. I threw in a "print $data" and it's just a blank screen, so it's not pulling anything... Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022037 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 Current Code: function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,"energy")); mysql_close($dbh); $data = str_replace("'","'",$data); return $data;} print $data Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022039 Share on other sites More sharing options...
mikesta707 Posted March 5, 2010 Share Posted March 5, 2010 I don't see where you even call that function in the code you posted. if you never call the function, the query never runs. Also, where is $user defined? are you sure $user is an object? Are you getting any errors from that page? try adding error_reporting(E_ALL); ini_set('display_errors', 1); to the top of the page and see if you have any errors being spit back at you. edit: just saw updated code. perhaps you mean to do function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,"energy")); mysql_close($dbh); $data = str_replace("'","'",$data); return $data;} print fish('some username'); where some username is, you input whatever username you are trying to query for. Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022041 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 Thanks! I got this error: Notice: Undefined variable: data in /home/katarra/public_html/fishing.php on line 152 I can attach the full file if that would help everybody. Thanks for the help! I appreciate it! [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022044 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 I'm trying to do it so that the person logged in, it will automatically search for their username and then determine if there is enough energy for them to fish. So, how do I tell it to search for the current person's "character name" and find that row then look at the energy column? Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022046 Share on other sites More sharing options...
mikesta707 Posted March 5, 2010 Share Posted March 5, 2010 that happens because of this line print $data; I think you have an issue with variable scope, as data isn't defined in the scope you are trying to use it in. When you define a variable in a function, it isn't usable anywhere outside of the function. more info: http://php.net/manual/en/language.variables.scope.php Since you return data from the function, you want to call the function, and "catch" the returned $data variable. something like I posted above (the echo fish(..) thing) would allow you to echo the contents of $data that was returned, but you probably want to store it into a variable like so $data = fish('some username'); remember though, the $data I just initialized in the line above is completely different from the $data that is in your fish function. Also, in order for the query to be run, you NEED to call the function. When you define a function, it won't ever be run unless you call it. I think this is part of your problem also Edit: just saw your new post. How do you check if someone is logged in? Is it session based? If so, then you could use a session variable to hold the character name, and use that session variable when running the query Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022050 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 I don't use cookies anywhere, so no sessions. Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022062 Share on other sites More sharing options...
schilly Posted March 5, 2010 Share Posted March 5, 2010 ummmm where is your query?? function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,"energy")); mysql_close($dbh); $data = str_replace("&#39;","'",$data); return $data;} print fish('some username'); Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022064 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 I do have this function in another script... I don't know if this helps. *laughs* function users(){ $data = ""; $dbh=dbconnect() or die ("Userlist read error: " . mysql_error()."<br>"); mysql_select_db("katarra_game"); $result = mysql_query("SELECT * FROM users ORDER BY username"); $rows = mysql_num_rows($result); for ($i = 0; $i < $rows; $i++) $data[$i] = mysql_result($result,$i,"username"); mysql_close($dbh); return $data; } Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022068 Share on other sites More sharing options...
schilly Posted March 5, 2010 Share Posted March 5, 2010 nope it's a separate function so separate variables. function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); $result = mysql_query("SELECT energy FROM users WHERE username = '" . mysql_real_escape_string($username) . "'") OR die(mysql_error()); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,"energy")); mysql_close($dbh); $data = str_replace("&#39;","'",$data); return $data;} print fish('some username'); try that. make sure you enter a valid username for "some username" Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022074 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 Hmm... I'm now getting this error, as it can't seem to connect to my database now... Odd. Notice: Use of undefined constant katarra_katarra - assumed 'katarra_katarra' in /home/katarra/public_html/scripts/dbfunc.s on line 3 Notice: Use of undefined constant pword - assumed 'pword' in /home/katarra/public_html/scripts/dbfunc.s on line 3 Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022076 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 Okay, fixed that error, but now it's just printing out a blank screen. require('scripts/function.s'); error_reporting(E_ALL); ini_set('display_errors', 1); function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); $result = mysql_query("SELECT energy FROM users WHERE username = '" . mysql_real_escape_string($username) . "'") OR die(mysql_error()); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,"energy")); mysql_close($dbh); $data = str_replace("&#39;","'",$data); return $data;} print fish('zelig'); I also got this error: Notice: Undefined variable: user in /home/katarra/public_html/fishing.php on line 20 Notice: Trying to get property of non-object in /home/katarra/public_html/fishing.php on line 20 You have no energy left! Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022077 Share on other sites More sharing options...
mikesta707 Posted March 5, 2010 Share Posted March 5, 2010 im assuming you still have the if ($user->energy < 0){.... code after what you posted? Get rid of that. first off, $user is never defined (as evident by the first error you get) and because of that, you can't use it like an object. Im assuming the $data variable being returned by fish() is how much energy whoever username is passed in has. Well in that case changing the if to if (fish('zelig') <= 0){ //not enough energy } alternatively, instead of callign the fish() function directly in the if statement, you can store the value that it returns in a function if you will need it later on, IE $energy = fish('zelig'); if ($energy <= 0){ //wdo whatever } //later on in the script do something else with $energy if you want Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022082 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 Okay, made the change, but now it's telling me that I don't have enough energy (which is the fallback if it can't obviously read the correct row). Here is the code, complete, so far: <?php require('scripts/function.s'); error_reporting(E_ALL); ini_set('display_errors', 1); function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); $result = mysql_query("SELECT energy FROM users WHERE username = '" . mysql_real_escape_string($username) . "'") OR die(mysql_error()); if (mysql_num_rows($result) > 0) $data = stripslashes(mysql_result($result,0,"energy")); mysql_close($dbh); $data = str_replace("&#39;","'",$data); return $data;} $expgain = 1; $expgain2 = 2; $energy = fish('zelig'); if ($energy <= 0){ //wdo whatever echo "You have no energy left!" ; exit; } //later on in the script do something else with $energy if you want if ($_GET['act'] == "fish") { if ($user->pole < 1) { echo "You do not have a fishing pole!"; exit; } else { $effect[0] = "caught a fish. <a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $effect[1] = "caught a boot.<a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $effect[2] = "caught a big fish.<a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $effect[3] = "caught a fish.<a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $effect[4] = "caught a tire.<a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $effect[5] = "caught a tire.<a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $effect[6] = "caught a boot.<a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $effect[7] = "caught a Duck and took some damage.<a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $effect[8] = "caught a boot.<a href=\"fishing.php?act=fish\">Keep fishing</a> or <a href=\"game.php\">Leave</a>"; $randeffect = array_rand($effect); $randoutput = $effect[$randeffect]; $blurb = "<p>You throw in your line and you:<br>"; switch ($randeffect) { case 0: if ($expgain + $user->fish_exp >= $user->fish_maxexp) //Player gained a level! { //Update player, gained a level echo "<br /><b>Your fishing leveled up!</b>"; $newexp = $expgain + $user->fish_exp - $user->fish_maxexp; $query = $db->execute("update `users` set `fish_level`=?, `fish_maxexp`=?, `fish_exp`=?, `fish`=? where `id`=?", array($user->fish_level + 1, $user->fish_maxexp + 1, $newexp, $user->fish + $fish, $user->id)); } else { $query = $db->execute("update `users` set `energy`=?, `fish_exp`=?, `fish`=? where `id`=?", array($user->energy - 1, $user->fish_exp + 1, $user->fish + $fish, $user->id)); echo "$blurb $randoutput<p>"; } break; case 1: if ($user->pole > 0) { $query = $db->execute("update `users` set `energy`=? where `id`=?", array($user->energy - 1, $user->id)); echo "$blurb $randoutput<p>"; } break; case 2: if ($expgain + $user->fish_exp >= $user->fish_maxexp) //Player gained a level! { //Update player, gained a level echo "<br /><b>Your fishing leveled up!</b>"; $newexp = $expgain2 + $user->fish_exp - $user->fish_maxexp; $query = $db->execute("update `users` set `fish_level`=?, `fish_maxexp`=?, `fish_exp`=?, `fish`=? where `id`=?", array($user->fish_level + 1, $user->fish_maxexp + 1, $newexp, $user->fish + $fish2, $user->id)); } else { $query = $db->execute("update `users` set `energy`=?, `fish_exp`=?, `fish`=? where `id`=?", array($user->energy - 1, $user->fish_exp + 2, $user->fish + $fish2, $user->id)); echo "$blurb $randoutput<p>"; } break; case 3: if ($expgain + $user->fish_exp >= $user->fish_maxexp) //Player gained a level! { //Update player, gained a level echo "<br /><b>Your fishing leveled up!</b>"; $newexp = $expgain + $user->fish_exp - $user->fish_maxexp; $query = $db->execute("update `users` set `fish_level`=?, `fish_maxexp`=?, `fish_exp`=?, `fish`=? where `id`=?", array($user->fish_level + 1, $user->fish_maxexp + 1, $newexp, $user->fish + $fish, $user->id)); } else { $query = $db->execute("update `users` set `energy`=?, `fish_exp`=?, `fish`=? where `id`=?", array($user->energy - 1, $user->fish_exp + 1, $user->fish + $fish, $user->id)); echo "$blurb $randoutput<p>"; } break; case 4: if ($user->pole > 0) { $query = $db->execute("update `users` set `energy`=? where `id`=?", array($user->energy - 1, $user->id)); echo "$blurb $randoutput<p>"; } break; case 5: if ($user->pole > 0) { $query = $db->execute("update `users` set `energy`=? where `id`=?", array($user->energy - 1, $user->id)); echo "$blurb $randoutput<p>"; } break; case 6: if ($user->pole > 0) { $query = $db->execute("update `users` set `energy`=? where `id`=?", array($user->energy - 1, $user->id)); echo "$blurb $randoutput<p>"; } break; case 7: if ($user->pole > 0) { $query = $db->execute("update `users` set `hp`=?, `energy`=? where `id`=?", array($user->hp - 10, $user->energy - 1, $user->id)); echo "$blurb $randoutput<p>"; } break; case 8: if ($user->pole > 0) { $query = $db->execute("update `users` set `energy`=? where `id`=?", array($user->energy - 1, $user->id)); echo "$blurb $randoutput<p>"; } break; } } } echo "<a href=\"fishing.php?act=fish\">Start fishing!</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022085 Share on other sites More sharing options...
mikesta707 Posted March 5, 2010 Share Posted March 5, 2010 try echoing $energy and see what data it has Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022090 Share on other sites More sharing options...
katarra Posted March 5, 2010 Author Share Posted March 5, 2010 Shows a blank screen. Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022092 Share on other sites More sharing options...
mikesta707 Posted March 5, 2010 Share Posted March 5, 2010 are you sure that a username named "zelig" exists in the table? You would have a blank screen if $data was empty. try changing your function to function fish($username){ $data = ""; $dbh=dbconnect() or die ('Fish error: ' . mysql_error()."<br>"); mysql_select_db("katarra_game"); $result = mysql_query("SELECT energy FROM users WHERE username = '" . mysql_real_escape_string($username) . "'") OR die(mysql_error()); if (mysql_num_rows($result) > 0){ $data = stripslashes(mysql_result($result,0,"energy")); } else { echo "No rows returned"; return 0; } mysql_close($dbh); $data = str_replace("&#39;","'",$data); return $data;} for debugging purposes. If the message "No rows returned" pops up, then your query isn't returning any results Quote Link to comment https://forums.phpfreaks.com/topic/194180-php-coding-error-help/#findComment-1022093 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.