Jump to content
MakeWebGames

Job Help!


Recommended Posts

Okay I need help, I have a V1 jobs. And I always get a error when I try to get promoted

Error -

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/****/public_html/****/job.php on line 128

 

Line 128 -

if(mysql_num_rows($q) == 0)

 

Line 123 - 140

function job_promote()
{
global $db,$ir,$c,$userid,$h;
$q=mysql_query("SELECT * FROM jobranks WHERE jrPAY > {$ir['jrPAY']} AND jrSTRN <= {$ir['strength']} AND jrLABOURN <= {$ir['labour']} AND jrIQN <= {$ir['IQ']} AND jrJOB = {$ir['job']} ORDER BY jrPAY DESC LIMIT 1");

if(mysql_num_rows($q) == 0)
{
print "<hr width='50%'>Please read the declined message.<hr width='50%'><h3>! DECLINED</h3>Sorry, you cannot be promoted at this time.<br/><br/>
<hr width='50%'><a href='job.php'>> Go Back</a><hr width='50%'>";
}
else
{
$r=mysql_num_rows($q);
mysql_query("UPDATE users SET jobrank={$r['jrID']} WHERE userid=$userid");
print "<hr width='50%'>Please read the successful message.<hr width='50%'><h3>! SUCCESSFUL</h3>Congrats, you have been promoted to {$r['jrNAME']}.<br/><br/>
<hr width='50%'><a href='job.php'>> Go Back</a><hr width='50%'>";
}
}
Link to comment
Share on other sites

so its saying that a Boolean has been given which means true/false what you can maybe try is:

if (! $q->EOF )
{
   print "<hr width='50%'>Please read the declined message.<hr width='50%'><h3>! DECLINED</h3>Sorry, you cannot be      promoted at this time.
   <hr width='50%'><a href='job.php'>> Go Back</a><hr width='50%'>";
}
else
{
   //$r=mysql_num_rows($q); no clue why that's in there
   mysql_query("UPDATE users SET jobrank={$r['jrID']} WHERE userid=$userid");
   print "<hr width='50%'>Please read the successful message.<hr width='50%'><h3>! SUCCESSFUL</h3>Congrats, you  have been promoted to {$r['jrNAME']}.
   <hr width='50%'><a href='job.php'>> Go Back</a><hr width='50%'>";
}
}

if it still errors post back and ill try to help but fair warning I'm mobile right now so just fiddle around with it and if it does the opposite of what you want remove the "!" sometimes I get confused :o

Link to comment
Share on other sites

No, it means exactly what it says

mysql_num_rows() expects parameter 1 to be resource, boolean given
Which clearly indicates that the $q is a boolean (as you correctly pointed out), however it needs to be a query resource which in turn indicates that the mysql_query statement itself failed.

Defensive programming suggests that you first define a variable to contain the sql statement, then pass that into mysql_query, and finally check the result ie:

// Preparation - makes the query below cleaner
$pay      = $ir['jrPAY'];
$strength = $ir['strength'];
$labour   = $ir['labour'];
$iq       = $ir['IQ'];
$job      = $ir['job'];

// Define the query
$sql = <<<SQL

  SELECT *
    FROM jobranks
   WHERE jrPAY > $pay
     AND jrSTRN <= $strength
     AND jrLABOURN <= $labour
     AND jrIQN <= $iq
     AND jrJOB = $job
ORDER BY jrPAY DESC
   LIMIT 1

SQL;

// Send the query top MySQL and retrieve the result (as a resource handle)
$q = mysql_query($sql);

// Check the result
if (!is_resource($q))
{
// If you are debugging, or running on a devlopment box, this is fine to
// leave in, but its not wise on production boxes.

echo 'MySQL query failed around line ' . __LINE__ . ' of ' . __FILE__ . '<br>';
echo 'Query was:<br>';
echo '<pre>' . $sql . '</pre>';
echo 'MySQL reports: ' . mysql_error();

exit;

}

// Now .. carry on as per normal.

Long winded? Yes, to an extent, but it will nail the bug in double quick time, it also helps if you are using a decent debugging tool like PHPStorm as you can clearly see which lines you can set a breakpoint on and examine the variables at that stage. Code should be designed to help the debugging and testing process after all its *you* how are going to have to read it and fix it when it breaks.

  • Like 1
Link to comment
Share on other sites

KyleMassacre & Octarine Thanks for the help! Really helped me alot guys. I got it fixed :')

 

function job_promote()
{
   global  $ir, $c, $userid, $h;
   $q =
          mysql_query(
                   "SELECT `jrID`,`jrNAME`
                    FROM `jobranks`
                    WHERE `jrPAY` > {$ir['jrPAY']}
                    AND `jrSTRN` <= {$ir['strength']}
                    AND `jrLABOURN` <= {$ir['labour']}
                    AND `jrIQN` <= {$ir['IQ']} AND `jrJOB` = {$ir['job']}
                    ORDER BY `jrPAY` DESC
                    LIMIT 1");
   if (mysql_num_rows($q) == 0)
   {
       echo "
	<hr width='50%'>Please read the declined message.<hr width='50%'><h3>! DECLINED</h3>Sorry, you cannot be promoted at this time.
   <hr width='50%'><a href='job.php'>> Go Back</a><hr width='50%'>
  		";
   }
   else
   {
       $r = mysql_fetch_array($q);
       mysql_query(
               "UPDATE `users`
                SET `jobrank` = {$r['jrID']}
                WHERE `userid` = $userid");
       echo "<hr width='50%'>Please read the successful message.<hr width='50%'><h3>! SUCCESSFUL</h3>Congrats, you  have been promoted to {$r['jrNAME']}.
   <hr width='50%'><a href='job.php'>> Go Back</a><hr width='50%'>
  		";
   }
   mysql_free_result($q);
}
Link to comment
Share on other sites

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.   Paste as plain text instead

  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...