Jump to content

Unsupported operand types


ElectricWizard

Recommended Posts

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&note=" . 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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by AbraCadaver
Link to comment
Share on other sites

 

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. 

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

 

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. 

Link to comment
Share on other sites

 

 

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&note=" . 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

The problem is here:

$url = "https://inputs.io/api?action=send&key=$apiKey&pin=$apiPin&note=" . urlencode($cashoutMessage .
 " | MyFaucet Powered") . "&address=" . urlencode($email) . "&amount=" . ($balance / 100000000);

Replace $balance with $row['balance']

Edited by AbraCadaver
Link to comment
Share on other sites

The problem is here:

$url = "https://inputs.io/api?action=send&key=$apiKey&pin=$apiPin&note=" . 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!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by ElectricWizard
Link to comment
Share on other sites

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