Dragon Blade Posted June 7, 2013 Posted June 7, 2013 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%'>"; } } Quote
KyleMassacre Posted June 7, 2013 Posted June 7, 2013 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 Quote
Octarine Posted June 8, 2013 Posted June 8, 2013 No, it means exactly what it says mysql_num_rows() expects parameter 1 to be resource, boolean givenWhich 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. 1 Quote
Dragon Blade Posted June 8, 2013 Author Posted June 8, 2013 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); } Quote
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.