Jump to content


Photo

Unsupported operand types


Best Answer AbraCadaver, 07 October 2013 - 07:20 AM

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']

Go to the full post


  • Please log in to reply
26 replies to this topic

#1 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 04 October 2013 - 01:41 PM

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!



#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,651 posts
  • LocationBonita, FL

Posted 04 October 2013 - 01:45 PM

var_dump($balanceQ->fetch_row()) before that line so you can see what value type it is returning.
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,888 posts
  • LocationThe Republic of Texas

Posted 04 October 2013 - 01:55 PM

$balanceQ->fetch_assoc() returns an associative array or null which will always be > or = 0. Maybe:

if($balanceQ->fetch_assoc()['balance'] >= 0){

Edited by AbraCadaver, 04 October 2013 - 01:57 PM.

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#4 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 04 October 2013 - 03:18 PM

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



#5 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,260 posts

Posted 04 October 2013 - 03:21 PM

why did you change the code in the first place? other than it using php5.4 syntax, there's nothing wrong with that line.


multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#6 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 04 October 2013 - 04:06 PM

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 



#7 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,260 posts

Posted 04 October 2013 - 04:10 PM

you mean like the same errors you got with the code from AbraCadaver's post?


multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#8 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,888 posts
  • LocationThe Republic of Texas

Posted 04 October 2013 - 04:11 PM

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, 04 October 2013 - 04:11 PM.

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#9 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 04 October 2013 - 05:11 PM

 

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. 



#10 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,888 posts
  • LocationThe Republic of Texas

Posted 04 October 2013 - 05:22 PM

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

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#11 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 04 October 2013 - 05:37 PM

 

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. 



#12 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,888 posts
  • LocationThe Republic of Texas

Posted 04 October 2013 - 06:01 PM

Not what I asked...


mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#13 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 04 October 2013 - 06:39 PM

Not what I asked...

 

Then I am not sure exactly what you mean. I am rather noobish. 



#14 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,888 posts
  • LocationThe Republic of Texas

Posted 06 October 2013 - 04:15 PM


 

Well damn, I've been chasing the wrong thing.  What is:

var_dump($balance);

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#15 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 06 October 2013 - 07:04 PM

 


 

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.



#16 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,651 posts
  • LocationBonita, FL

Posted 06 October 2013 - 07:09 PM

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.
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#17 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 06 October 2013 - 07:27 PM

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



#18 Zane

Zane

    Advanced Member

  • Administrators
  • 3,826 posts
  • LocationMurphy, NC

Posted 06 October 2013 - 08:16 PM

if($balanceQ->fetch_assoc()['balance'] >= 0){

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank

401299.png

#19 ElectricWizard

ElectricWizard

    Member

  • Members
  • PipPip
  • 18 posts

Posted 06 October 2013 - 08:38 PM

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"



#20 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,018 posts

Posted 07 October 2013 - 05:34 AM

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, 07 October 2013 - 05:34 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com