Jump to content

PHP coding error... Help!


katarra

Recommended Posts

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("&#39;","'",$data);	
return $data;}

$data = $user;
$expgain = 1;
$expgain2 = 2;

if ($user->energy <= 0) 
{
echo "You have no energy left!" ;
exit;
}

 

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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("&#39;","'",$data);	
return $data;}

print $data

Link to comment
Share on other sites

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("&#39;","'",$data);	
return $data;}

print fish('some username');

 

where some username is, you input whatever username you are trying to query for.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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("&#38;#39;","'",$data);	
return $data;}

print fish('some username');

 

 

Link to comment
Share on other sites

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;
}

 

Link to comment
Share on other sites

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("&#38;#39;","'",$data);	
return $data;}

print fish('some username');

 

try that. make sure you enter a valid username for "some username"

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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("&#38;#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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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("&#38;#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>";

?>

Link to comment
Share on other sites

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("&#38;#39;","'",$data);	
return $data;}

 

for debugging purposes. If the message "No rows returned" pops up, then your query isn't returning any results

 

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.