Pages:
Author

Topic: Question for the php-heads... - page 2. (Read 1436 times)

legendary
Activity: 1904
Merit: 1037
Trusted Bitcoiner
February 12, 2013, 01:30:48 AM
#9
This will run if $balance is > 0

No, it will run if $balance != 0. If $balance is less than 0 gmp_cmp will return -1, which is true.

Yes, that's actually correct. My bad - too late here. Smiley

The correct would then be:

Code:
if(gmp_cmp($balance, '0') > 0)

and making the $balance variable into a string before the comparison, or does somebody disagree ?



i have to disagree

idk...  but i think he is expecting $balance to be a string from that start

he says the code works when he uses $bitcoin->getbalance, the exact same code but with  $litecoin->getbalance doesn't work

so i don't think forcing the return value to be a string is the answer, understanding why  $litecoin->getbalance isn't returning a string should make the problem clear

do me $balance is null...


why use gmp_cmp(), don't you think $litecoin->getbalance() would return a float?
just do
Code:
if($balance > 0)
hero member
Activity: 868
Merit: 1000
February 12, 2013, 01:11:54 AM
#8
This will run if $balance is > 0

No, it will run if $balance != 0. If $balance is less than 0 gmp_cmp will return -1, which is true.

Yes, that's actually correct. My bad - too late here. Smiley

The correct would then be:

Code:
if(gmp_cmp($balance, '0') > 0)

and making the $balance variable into a string before the comparison, or does somebody disagree ?

administrator
Activity: 5222
Merit: 13032
February 12, 2013, 01:08:13 AM
#7
This will run if $balance is > 0

No, it will run if $balance != 0. If $balance is less than 0 gmp_cmp will return -1, which is true.
hero member
Activity: 868
Merit: 1000
February 12, 2013, 01:04:39 AM
#6
thanks cosmicone, means that brain is still working.

OP said:

Code:
Warning: gmp_cmp(): Unable to convert variable to GMP

I looked up the manual entry.

When it comes to the variable $balance, try to make it into a string before you run it through gmp_cmp.

This is how you would assing a balance to it:

Code:
$balance = 1.0; // assigns 1.0 to $balance.
$balance = 1.0.''; // assigns 1.0 as a string to $balance

Then you can compare by doing:

Code:
var_dump(gmp_cmp($balance, '0'));

Remove the var_dump for the production code, but that will give you the right output from gmp_cmp.

Refer to the manual linked above for what the return codes mean.


So changing

Code:
$balance = $litecoin->getbalance($uid, 6);

into

Code:
$balance = ($litecoin->getbalance($uid, 6)).'';

should do it then.
member
Activity: 105
Merit: 10
February 12, 2013, 12:58:30 AM
#5
Herodes has it right...


if(gmp_cmp("$balance", "0")) {

This will run if $balance is > 0

 } else {

This runs if it is 0 or less than.  

 }

So you would have

Code:
function sync_to_litecoin($uid)
{
    $litecoin = connect_litecoin();
    $balance = $litecoin->getbalance($uid, 6);
    $query = "
        UPDATE purses
        SET amount = amount + '$balance'
        WHERE uid='$uid' AND type='LTC';
    ";
    do_query($query);
    if (gmp_cmp($balance, '0')) {
        $litecoin->move($uid, '', $balance);
        $query = "
            INSERT INTO requests (req_type, uid, amount, curr_type)
            VALUES ('DEPOS', '$uid', '$balance', 'LTC');
        ";
        do_query($query);
    }
}
legendary
Activity: 1904
Merit: 1037
Trusted Bitcoiner
February 12, 2013, 12:54:43 AM
#4
i'd var_dump $balance in both cases and look whats different about them

gmp_cmp() can't convert the $balance variable returned by $litecoin->getbalance($uid, 6);

my guess is $litecoin->getbalance($uid, 6); fails is some way and returns null, or isn't returning the same thing as $balance = $bitcoin->getbalance($uid, 6);
hero member
Activity: 868
Merit: 1000
February 12, 2013, 12:49:37 AM
#3
Code:
if (gmp_cmp($balance, '0') > 0)


I take it that you're trying to check if the returned value of the balance compared to 0 is higher than 0, ie 1.

A conditional statement is true if it equals 1, so you may try something like:

Code:
if(gmp_cmp("$balance", "0"))

Let me know if it helped, and works as you intended it to work.


administrator
Activity: 5222
Merit: 13032
February 12, 2013, 12:48:47 AM
#2
var_dump before gmp_cmp.

My guess is that the second do_query returns null or something like that.
sr. member
Activity: 294
Merit: 250
February 12, 2013, 12:17:03 AM
#1
Alright... I have been fighting with this for days and I can't take it anymore...

Can anyone tell me why this works:

Code:
function sync_to_bitcoin($uid)
{
    $bitcoin = connect_bitcoin();
    $balance = $bitcoin->getbalance($uid, 6);
    $query = "
        UPDATE purses
        SET amount = amount + '$balance'
        WHERE uid='$uid' AND type='BTC';
    ";
    do_query($query);
    if (gmp_cmp($balance, '0') > 0) {
        $bitcoin->move($uid, '', $balance);
        $query = "
            INSERT INTO requests (req_type, uid, amount, curr_type)
            VALUES ('DEPOS', '$uid', '$balance', 'BTC');
        ";
        do_query($query);
    }
}

But this:

Code:
function sync_to_litecoin($uid)
{
    $litecoin = connect_litecoin();
    $balance = $litecoin->getbalance($uid, 6);
    $query = "
        UPDATE purses
        SET amount = amount + '$balance'
        WHERE uid='$uid' AND type='LTC';
    ";
    do_query($query);
    if (gmp_cmp($balance, '0') > 0) {
        $litecoin->move($uid, '', $balance);
        $query = "
            INSERT INTO requests (req_type, uid, amount, curr_type)
            VALUES ('DEPOS', '$uid', '$balance', 'LTC');
        ";
        do_query($query);
    }
}

is returning:

Code:
Warning: gmp_cmp(): Unable to convert variable to GMP

I am trying to use them in-line as:

Code:
[code]function sync_to_bitcoin($uid)
{
    $bitcoin = connect_bitcoin();
    $balance = $bitcoin->getbalance($uid, 6);
    $query = "
        UPDATE purses
        SET amount = amount + '$balance'
        WHERE uid='$uid' AND type='BTC';
    ";
    do_query($query);
    if (gmp_cmp($balance, '0') > 0) {
        $bitcoin->move($uid, '', $balance);
        $query = "
            INSERT INTO requests (req_type, uid, amount, curr_type)
            VALUES ('DEPOS', '$uid', '$balance', 'BTC');
        ";
        do_query($query);
    }
}

function sync_to_litecoin($uid)
{
    $litecoin = connect_litecoin();
    $balance = $litecoin->getbalance($uid, 6);
    $query = "
        UPDATE purses
        SET amount = amount + '$balance'
        WHERE uid='$uid' AND type='LTC';
    ";
    do_query($query);
    if (gmp_cmp($balance, '0') > 0) {
        $litecoin->move($uid, '', $balance);
        $query = "
            INSERT INTO requests (req_type, uid, amount, curr_type)
            VALUES ('DEPOS', '$uid', '$balance', 'LTC');
        ";
        do_query($query);
    }
}
Any help is greatly appreciated...[/code]
Pages:
Jump to: