ElectricWizard Posted October 4, 2013 Share Posted October 4, 2013 I am running a bitcoin faucet, and people are getting an "unsupported operand types" error in one of my files on line 88. Here is that section of code: if($balance >= $cashout){ $mysqli->query("UPDATE balances SET balance=balance-$balance WHERE email='$email'"); $balanceQ = $mysqli->query("SELECT balance FROM balances WHERE email='$email'"); //we check again to prevent race attacks if($balanceQ->fetch_assoc() >= 0){ $url = "https://inputs.io/api?action=send&key=$apiKey&pin=$apiPin¬e=" . urlencode($cashoutMessage . " | MyFaucet Powered") . "&address=" . urlencode($email) . "&amount=" . ($balance / 100000000); $response = file_get_contents($url); if($response[0] == "["){ //success echo "<div class='alert alert-success'>Successful cashout to $email - enjoy!</div>"; } else { echo "<div class='alert alert-error'>An error has occured - $response</div>"; if($response == "NO_BALANCE"){ echo "<div class='alert alert-error'>The site does not have enough coins to pay out!</div>"; $mysqli->query("UPDATE balances SET balance=balance+$balance WHERE email='$email'"); I am wondering what is wrong and how to fix it. The only part of this code I modified was line 87, which used to be: if($balanceQ->fetch_row()[0] >= 0){ Thanks in advance for any assistance! Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/ Share on other sites More sharing options...
kicken Posted October 4, 2013 Share Posted October 4, 2013 var_dump($balanceQ->fetch_row()) before that line so you can see what value type it is returning. Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452618 Share on other sites More sharing options...
AbraCadaver Posted October 4, 2013 Share Posted October 4, 2013 (edited) $balanceQ->fetch_assoc() returns an associative array or null which will always be > or = 0. Maybe: if($balanceQ->fetch_assoc()['balance'] >= 0){ Edited October 4, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452621 Share on other sites More sharing options...
ElectricWizard Posted October 4, 2013 Author Share Posted October 4, 2013 var_dump($balanceQ->fetch_row()) before that line so you can see what value type it is returning. This gave me: unexpected T_VARIABLE in /home/*****/public_html/********/faucet.php on line 88 $balanceQ->fetch_assoc() returns an associative array or null which will always be > or = 0. Maybe: if($balanceQ->fetch_assoc()['balance'] >= 0){ This one gave me: syntax error, unexpected '[' in /home/**********/public_html/*******************/faucet.php on line 87 Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452628 Share on other sites More sharing options...
mac_gyver Posted October 4, 2013 Share Posted October 4, 2013 why did you change the code in the first place? other than it using php5.4 syntax, there's nothing wrong with that line. Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452630 Share on other sites More sharing options...
ElectricWizard Posted October 4, 2013 Author Share Posted October 4, 2013 why did you change the code in the first place? other than it using php5.4 syntax, there's nothing wrong with that line. It kept giving me errors about that, and changing it was the only way it would stop giving errors. I have no idea why Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452635 Share on other sites More sharing options...
mac_gyver Posted October 4, 2013 Share Posted October 4, 2013 you mean like the same errors you got with the code from AbraCadaver's post? Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452637 Share on other sites More sharing options...
AbraCadaver Posted October 4, 2013 Share Posted October 4, 2013 (edited) It kept giving me errors about that, and changing it was the only way it would stop giving errors. I have no idea why Because you don't have PHP 5.4? I guess we assumed it worked with fetch_row() and for some reason you changed it to fetch_assoc(). Would've been helpful to know that the original code didn't work either Try: $row = $balanceQ->fetch_row(); if($row[0] >= 0){ or: $row = $balanceQ->fetch_assoc(); if($row['balance'] >= 0){ Edited October 4, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452638 Share on other sites More sharing options...
ElectricWizard Posted October 4, 2013 Author Share Posted October 4, 2013 Because you don't have PHP 5.4? I guess we assumed it worked with fetch_row() and for some reason you changed it to fetch_assoc(). Would've been helpful to know that the original code didn't work either Try: $row = $balanceQ->fetch_row(); if($row[0] >= 0){ or: $row = $balanceQ->fetch_assoc(); if($row['balance'] >= 0){ Both of these give me "Unsupported operand types in blahblah on line 89". I am replacing line 87 with this, right? (just making sure I am doing the editing correctly). Replacing line 87 pushes the original line 88 to 89, so it's basically the same error for some reason. I forgot to mention this error occurs when someone tries to cash out. The top half of the page loads properly when the cashout button is hit, but then that error is in the centre of the page. Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452640 Share on other sites More sharing options...
AbraCadaver Posted October 4, 2013 Share Posted October 4, 2013 Both of these give me "Unsupported operand types in blahblah on line 89". I am replacing line 87 with this, right? (just making sure I am doing the editing correctly). Replacing line 87 pushes the original line 88 to 89, so it's basically the same error for some reason. I forgot to mention this error occurs when someone tries to cash out. The top half of the page loads properly when the cashout button is hit, but then that error is in the centre of the page. Well damn, I've been chasing the wrong thing. What is: var_dump($balance); Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452642 Share on other sites More sharing options...
ElectricWizard Posted October 4, 2013 Author Share Posted October 4, 2013 Well damn, I've been chasing the wrong thing. What is: var_dump($balance); The idea of this script is, to cashout, it checks someone's balance against what is in the database associated with their inputs.io email address, and if it meets a certain threshold, it gives them a payout. The threshold is 10,000, so if their balance is that or higher, it will use my inputs.io account to pay them (using the inputs.io API). When a successful cashout occurs, it gives a confirmation message. The person who brought this error to my attention was trying to cash out when he discovered it, and I know he has a high enough balance to cash out. Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452643 Share on other sites More sharing options...
AbraCadaver Posted October 4, 2013 Share Posted October 4, 2013 Not what I asked... Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452645 Share on other sites More sharing options...
ElectricWizard Posted October 4, 2013 Author Share Posted October 4, 2013 Not what I asked... Then I am not sure exactly what you mean. I am rather noobish. Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452648 Share on other sites More sharing options...
AbraCadaver Posted October 6, 2013 Share Posted October 6, 2013 Well damn, I've been chasing the wrong thing. What is: var_dump($balance); Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452852 Share on other sites More sharing options...
ElectricWizard Posted October 7, 2013 Author Share Posted October 7, 2013 Well damn, I've been chasing the wrong thing. What is: var_dump($balance); Okay, since I'm still not entirely clear exactly what var_dump means, I will include the part of faucet.php that addresses $balance. I added it to the beginning of the code I pasted into the OP; the new part is line 73 - 83, and the added part establishes the parameters which $balance is measured against. if(isset($_POST['cashout'])){ // ok, cash out $balanceQ = $mysqli->query("SELECT balance FROM balances WHERE email='$email'"); if($balanceQ->num_rows){ $balance = $balanceQ->fetch_assoc(); if($balance < 10000){ echo "CONFIG ERROR: Amount is too small"; exit; } if($balance >= $cashout){ $mysqli->query("UPDATE balances SET balance=balance-$balance WHERE email='$email'"); $balanceQ = $mysqli->query("SELECT balance FROM balances WHERE email='$email'"); //we check again to prevent race attacks $row = $balanceQ->fetch_assoc(); if($row['balance'] >= 0){ $url = "https://inputs.io/api?action=send&key=$apiKey&pin=$apiPin¬e=" . urlencode($cashoutMessage . " | MyFaucet Powered") . "&address=" . urlencode($email) . "&amount=" . ($balance / 100000000); $response = file_get_contents($url); if($response[0] == "["){ //success echo "<div class='alert alert-success'>Successful cashout to $email - enjoy!</div>"; } else { echo "<div class='alert alert-error'>An error has occured - $response</div>"; if($response == "NO_BALANCE"){ echo "<div class='alert alert-error'>The site does not have enough coins to pay out!</div>"; $mysqli->query("UPDATE balances SET balance=balance+$balance WHERE email='$email'"); } If this is not what you wanted, I am sorry. Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452868 Share on other sites More sharing options...
kicken Posted October 7, 2013 Share Posted October 7, 2013 Okay, since I'm still not entirely clear exactly what var_dump means, var_dump is a function for debugging what a variable contains. What you've been asked to do is add a line of code which uses var dump to show you what $balance contains. Add the line of code you were given after line 88 and let us know what the output is. Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452871 Share on other sites More sharing options...
ElectricWizard Posted October 7, 2013 Author Share Posted October 7, 2013 var_dump is a function for debugging what a variable contains. What you've been asked to do is add a line of code which uses var dump to show you what $balance contains. Add the line of code you were given after line 88 and let us know what the output is. The result was: array(1) { ["balance"]=> string(5) "14300" } 14300 is the balance of my test email address Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452874 Share on other sites More sharing options...
Zane Posted October 7, 2013 Share Posted October 7, 2013 if($balanceQ->fetch_assoc()['balance'] >= 0){ Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452879 Share on other sites More sharing options...
ElectricWizard Posted October 7, 2013 Author Share Posted October 7, 2013 if($balanceQ->fetch_assoc()['balance'] >= 0){ That's what I should replace line 88 with? If so, it gave me "Parse error: syntax error, unexpected '[' in ******* on line 88" Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452882 Share on other sites More sharing options...
Ch0cu3r Posted October 7, 2013 Share Posted October 7, 2013 (edited) That's what I should replace line 88 with? If so, it gave me "Parse error: syntax error, unexpected '[' in ******* on line 88" Are you running PHP version 5.4? To check the php version run echo phpversion(); The code being suggested to use will only work on PHP5.4 and onwards. Edited October 7, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452910 Share on other sites More sharing options...
AbraCadaver Posted October 7, 2013 Share Posted October 7, 2013 (edited) The problem is here: $url = "https://inputs.io/api?action=send&key=$apiKey&pin=$apiPin¬e=" . urlencode($cashoutMessage . " | MyFaucet Powered") . "&address=" . urlencode($email) . "&amount=" . ($balance / 100000000); Replace $balance with $row['balance'] Edited October 7, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452915 Share on other sites More sharing options...
ElectricWizard Posted October 7, 2013 Author Share Posted October 7, 2013 The problem is here: $url = "https://inputs.io/api?action=send&key=$apiKey&pin=$apiPin¬e=" . urlencode($cashoutMessage . " | MyFaucet Powered") . "&address=" . urlencode($email) . "&amount=" . ($balance / 100000000); Replace $balance with $row['balance'] I tested it using one of my other email addresses, and it looks like it worked, thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1452995 Share on other sites More sharing options...
mac_gyver Posted October 15, 2013 Share Posted October 15, 2013 i'm going to bump this thread, based on your additional thread/problem (it's probably caused by the same problem as in this code), by mentioning that the error in this thread is/was due to line 77, in post #15. did you by any chance alter line 77 of the code too, to try and fix an error that was occurring at it? if so, what was the original code and what was the error? the current code is fetching an array into $balance, not the value that the query returned. i'm also pretty sure you didn't ever check and show us the php version, that has been mentioned more than once as a possible problem with the code. Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1453985 Share on other sites More sharing options...
ElectricWizard Posted October 20, 2013 Author Share Posted October 20, 2013 (edited) i'm going to bump this thread, based on your additional thread/problem (it's probably caused by the same problem as in this code), by mentioning that the error in this thread is/was due to line 77, in post #15. did you by any chance alter line 77 of the code too, to try and fix an error that was occurring at it? if so, what was the original code and what was the error? the current code is fetching an array into $balance, not the value that the query returned. i'm also pretty sure you didn't ever check and show us the php version, that has been mentioned more than once as a possible problem with the code. When I check the version of php, I get "5.3.27". Weird, though, since I changed it so that it would use 5.5 but that clearly didn't work. Yes I changed that line before. It was an error I was getting on most instances of that line and similar. It was first "Parse error: syntax error, unexpected '[' in /home/*************/public_html/config.php on line 21". The line was: $rewards = [1000, 1500, 2000, 2500, 3000, 3500, 4000, 5000]; The guy who helped me with it said to change that line to this: $rewards = array(1000, 1500, 2000, 2500, 3000, 3500, 4000, 5000); That fixed that error, but any other line that used what I understand to be the new format of php gave me a similar error, including line 77 of faucet.php. Line 77 used to be: $balance = $balanceQ->fetch_row()[0]; But it gave the "unexpected [" error as well. So I went through and changed all of those lines so that it would not give me errors anymore. Line 77 became: $balance = $balanceQ->fetch_assoc(); Obviously that was not the correct solution, though. But it definitely seems it's a problem with the php version. My hosting control panel has a way of changing the version of php used, but changing it to 5.5 did not work. Edited October 20, 2013 by ElectricWizard Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1454584 Share on other sites More sharing options...
mac_gyver Posted October 20, 2013 Share Posted October 20, 2013 you cannot just make random changes to code unless you know the change produces the intended result. the php5.4 syntax $balanceQ->fetch_row()[0]; calls the ->fetch_row() method and references the zeroth element of the array that was returned. the replacement code must perform that same operation. you could use either of the following - $row = $balanceQ->fetch_row(); $balance = $row[0]; or list($balance) = $balanceQ->fetch_row(); Quote Link to comment https://forums.phpfreaks.com/topic/282719-unsupported-operand-types/#findComment-1454587 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.