Jump to content

Recommended Posts

Just put together my cronjob, was working fine but wanted it to just update a row if it existed already with the same quality value instead of inserting a whole new row, for some reason i'm getting a;

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\public_html\crons\GrowCron.php on line 17

 

Error, Line 17 is

 

$sql3 = "SELECT * FROM player_inventory WHERE player_id = '$res2[id]' AND quality = '$Quality'";

$num2 = mysql_num_rows(mysql_query($sql3));

if($num2 == 0){

 

Any clues?

 

<?php
include("server.php");
$sql2 = "SELECT * FROM players";
$que2 = mysql_query($sql2);
while($res2=mysql_fetch_array($que2)) {
$sql = "SELECT * FROM player_inventory WHERE item_id = 17 AND player_id = $res2[id]";
$que = mysql_query($sql) or die(mysql_error());
$res = mysql_fetch_array($que);
$num = mysql_num_rows($que);
$Weight = 8 / 100 * (100 + $res2['ProductionWeight']) * $res['quantity'];
$Quality = 75 / 100 * (100 + $res2['ProductionWeight']);
$StreetCred = $Weight * $Quality / 10;
if($num >= 1){
$update = "UPDATE players SET StreetCred = StreetCred + $StreetCred";
mysql_query($update) or die(mysql_error());;
$sql3 = "SELECT * FROM player_inventory WHERE player_id = '$res2[id]' AND quality = '$Quality'";
$num2 = mysql_num_rows(mysql_query($sql3));
if($num2 == 0){
$update2 = "INSERT INTO drug_inventory (player_id,quality,weight) VALUES('$res2[id]',$Quality,$Weight)";
mysql_query($update2) or die(mysql_error());;
}else{
//row exists, update it.
$sql5 = "UPDATE drug_inventory SET weight = $weight WHERE player_id = '$res2[id]' AND quality = '$Quality'";
mysql_query($sql5) or die(mysql_error());;
echo "Grow Completed.";
}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/255553-dohhhh/
Share on other sites

Rightio, i fixed that error, only issue is it now only updates 1 player not all the players if the row already exists for said player, im really just plain lost as to why, below is the new code, any help would really be appreciated guys

 

<?php
include("server.php");
$sql = "SELECT * FROM players";
$que = mysql_query($sql);
while($res=mysql_fetch_array($que)) {
$sql2 = "SELECT * FROM player_inventory WHERE item_id = '17' AND player_id = '$res[id]'";
$que2 = mysql_query($sql2) or die(mysql_error());
$res2 = mysql_fetch_array($que2);
$num = mysql_num_rows($que2);
$Weight = 8 / 100 * (100 + $res['ProductionWeight']) * $res2['quantity'];
$Quality = 75 / 100 * (100 + $res['ProductionWeight']);
$StreetCred = $Weight * $Quality / 10;
$sql3 = "SELECT * FROM drug_inventory WHERE player_id = '$res[id]' AND quality = '$Quality'";
$que3 = mysql_query($sql3) or die(mysql_error());
$res3 = mysql_fetch_array($que3);
$num2 = mysql_num_rows($que3);
if($num >= 1 && $num2 >= 1){
$sql = "UPDATE drug_inventory SET weight = weight + '$Weight' WHERE quality = '$Quality'  AND player_id = '$res[id]'";
mysql_query($sql) or die(mysql_error());;
}else{
if($num >= 1 && $num2 == 0){
$update2 = "INSERT INTO drug_inventory (player_id,quality,weight) VALUES('$res[id]',$Quality,$Weight)";
mysql_query($update2) or die(mysql_error());;
echo "Grow Completed.";
}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/255553-dohhhh/#findComment-1310176
Share on other sites

Have you tried debugging this code at all?

e.g. echoing your SQL statements and comparing them to your database data.

If your script is only updating one player instead of every player, as it should, then your condition is failing. Comparing the SQL to the database may give you some insight as to why.

Link to comment
https://forums.phpfreaks.com/topic/255553-dohhhh/#findComment-1310185
Share on other sites

Its not failing per say, its really odd, its updating one player, and then just inserting a new entry for the others, it kind of has to be working somehow to initially update that 1 player, no?

yes, by failing what I mean to say is that the condition is no longer being met after the initial iteration, we need to figure out why this is happening. I would output my SQl statements and check them, then I would output $num and $num2 to see what their values are every iteration.

Link to comment
https://forums.phpfreaks.com/topic/255553-dohhhh/#findComment-1310192
Share on other sites

Grow Completed. 1 1

Thats echoing $num1 and $num2 so in theory it should work??

<?php
include("server.php");
$sql = "SELECT * FROM players";
$que = mysql_query($sql);
while($res=mysql_fetch_array($que)) {
           $sql2 = "SELECT * FROM player_inventory WHERE item_id = '17' AND player_id = '$res[id]'";
           $que2 = mysql_query($sql2) or die(mysql_error());
           $res2 = mysql_fetch_array($que2);
           $num = mysql_num_rows($que2);
                        $Weight = 8 / 100 * (100 + $res['ProductionWeight']) * $res2['quantity'];
                        $Quality = 75 / 100 * (100 + $res['ProductionWeight']);
                         $StreetCred = $Weight * $Quality / 10;
                                           $sql3 = "SELECT * FROM drug_inventory WHERE quality = '$Quality' AND player_id = '$res2[player_id]'";
                                           $que3 = mysql_query($sql3) or die(mysql_error());
                                           $res3 = mysql_fetch_array($que3);
                                           $num2 = mysql_num_rows($que3);
if($num >= 1){
if($num2 == 0){
$update2 = "INSERT INTO drug_inventory (player_id,quality,weight) VALUES('$res[id]',$Quality,$Weight)";
mysql_query($update2) or die(mysql_error());;
}else{
if($num2 >= 1){
$sql = "UPDATE drug_inventory SET weight = weight + '$Weight' WHERE quality = '$Quality'  AND player_id = '$res[id]'";
mysql_query($sql) or die(mysql_error());;
echo "Grow Completed. $num $num2";
}
}
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/255553-dohhhh/#findComment-1310314
Share on other sites

well, according to your conditions, it is reaching the point of updating every iteration as it should. This makes me believe that something is not right in the query itself. Perhaps one of the variables in the query is not correct, which would result in the query returning true, but the query will not update anything.. echo the UPDATE $sql every iteration to check for errors.

Link to comment
https://forums.phpfreaks.com/topic/255553-dohhhh/#findComment-1310317
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.