Bitcoin Forum
May 19, 2019, 11:39:26 PM *
News: Latest Bitcoin Core release: 0.18.0 [Torrent] (New!)
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: Question for the php-heads...  (Read 1318 times)
c4n10
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250



View Profile
February 12, 2013, 04: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]
1558309166
Hero Member
*
Offline Offline

Posts: 1558309166

View Profile Personal Message (Offline)

Ignore
1558309166
Reply with quote  #2

1558309166
Report to moderator
START MINING BTC NOW WITH NEW GENERATION S17 ANTMINER! BTC/BCH/LTC/ZCASH/DASH/ETH/DCR MINING PLAN AVAILABLE Highly Reduced Electricity Fee $0.067/T/DAY!
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1558309166
Hero Member
*
Offline Offline

Posts: 1558309166

View Profile Personal Message (Offline)

Ignore
1558309166
Reply with quote  #2

1558309166
Report to moderator
1558309166
Hero Member
*
Offline Offline

Posts: 1558309166

View Profile Personal Message (Offline)

Ignore
1558309166
Reply with quote  #2

1558309166
Report to moderator
1558309166
Hero Member
*
Offline Offline

Posts: 1558309166

View Profile Personal Message (Offline)

Ignore
1558309166
Reply with quote  #2

1558309166
Report to moderator
theymos
Administrator
Legendary
*
Offline Offline

Activity: 3388
Merit: 5521


View Profile
February 12, 2013, 04:48:47 AM
 #2

var_dump before gmp_cmp.

My guess is that the second do_query returns null or something like that.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
Herodes
Hero Member
*****
Offline Offline

Activity: 868
Merit: 1000


View Profile
February 12, 2013, 04: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.


adamstgBit
Legendary
*
Offline Offline

Activity: 1904
Merit: 1002


Trusted Bitcoiner


View Profile WWW
February 12, 2013, 04: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);

cosmicone
Member
**
Offline Offline

Activity: 105
Merit: 10



View Profile
February 12, 2013, 04: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);
    }
}
Herodes
Hero Member
*****
Offline Offline

Activity: 868
Merit: 1000


View Profile
February 12, 2013, 05: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.
theymos
Administrator
Legendary
*
Offline Offline

Activity: 3388
Merit: 5521


View Profile
February 12, 2013, 05: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.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
Herodes
Hero Member
*****
Offline Offline

Activity: 868
Merit: 1000


View Profile
February 12, 2013, 05: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 ?

adamstgBit
Legendary
*
Offline Offline

Activity: 1904
Merit: 1002


Trusted Bitcoiner


View Profile WWW
February 12, 2013, 05:30:48 AM
Last edit: February 12, 2013, 05:50:09 AM by adamstgBit
 #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)

c4n10
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250



View Profile
February 12, 2013, 05:51:45 AM
 #10

Ok, thank you for all the answers guys! The errors are gone but it's not updating my database properly. I believe it has to do with the call to the "requests" table not functioning properly as the requests table reports as "empty set" in mysql...
Herodes
Hero Member
*****
Offline Offline

Activity: 868
Merit: 1000


View Profile
February 12, 2013, 06:12:30 AM
 #11

Ok, thank you for all the answers guys! The errors are gone but it's not updating my database properly. I believe it has to do with the call to the "requests" table not functioning properly as the requests table reports as "empty set" in mysql...

to debug it, echo the mysql queries and run them by hand through mysql and weed out the errors.

adam, thanks for the input, brain works badly when it's late.
adamstgBit
Legendary
*
Offline Offline

Activity: 1904
Merit: 1002


Trusted Bitcoiner


View Profile WWW
February 12, 2013, 06:23:00 AM
 #12

Ok, thank you for all the answers guys! The errors are gone but it's not updating my database properly. I believe it has to do with the call to the "requests" table not functioning properly as the requests table reports as "empty set" in mysql...

to debug it, echo the mysql queries and run them by hand through mysql and weed out the errors.

adam, thanks for the input, brain works badly when it's late.
I know the feeling   Tongue
weird its complaining about gmp_cmp() when its the do_query that failed.


what is this "connect_bitcoin();"

is their a php lib for managing a bitcoin wallet?

c4n10
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250



View Profile
February 12, 2013, 06:31:26 AM
 #13

When I run:
Code:
INSERT INTO requests (req_type, uid, amount, curr_type) VALUES ('DEPOS', '169', '1', 'LTC');
from mysql command line it works.

but for some reason when the page runs that script it doesn't work...
c4n10
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250



View Profile
February 12, 2013, 06:38:18 AM
 #14

Ok, thank you for all the answers guys! The errors are gone but it's not updating my database properly. I believe it has to do with the call to the "requests" table not functioning properly as the requests table reports as "empty set" in mysql...

to debug it, echo the mysql queries and run them by hand through mysql and weed out the errors.

adam, thanks for the input, brain works badly when it's late.
I know the feeling   Tongue
weird its complaining about gmp_cmp() when its the do_query that failed.


what is this "connect_bitcoin();"

is their a php lib for managing a bitcoin wallet?

lol, connect_bitcoin is a separate function that calls to a script that connects to the servers bitcoin port, it is used in tandem with other scripts to communicate with the daemon and return the results of various rpc functions depending on the script it is being used in. What I'm doing is I've built an intersango installation, removed the "GBP" stuff, have added litecoin and bitcoin to the code, downloaded the "exper" branch from intersango's gitorious, imported the new database from exper which contains the "baskets" table where I added litecoin and bitcoin, now just trying to get it to update the db properly...
Herodes
Hero Member
*****
Offline Offline

Activity: 868
Merit: 1000


View Profile
February 12, 2013, 06:44:50 AM
 #15

When I run:
Code:
INSERT INTO requests (req_type, uid, amount, curr_type) VALUES ('DEPOS', '169', '1', 'LTC');
from mysql command line it works.

but for some reason when the page runs that script it doesn't work...

echo your query statement in php. then copy it to mysql and run it, there may be some small errors, like not adding quotes around strings.
adamstgBit
Legendary
*
Offline Offline

Activity: 1904
Merit: 1002


Trusted Bitcoiner


View Profile WWW
February 12, 2013, 06:52:37 AM
 #16

When I run:
Code:
INSERT INTO requests (req_type, uid, amount, curr_type) VALUES ('DEPOS', '169', '1', 'LTC');
from mysql command line it works.

but for some reason when the page runs that script it doesn't work...

echo your query statement in php. then copy it to mysql and run it, there may be some small errors, like not adding quotes around strings.

try hard coding the values as a test and  do
Code:
echo mysql_error();
right after your do_query

and stop coding at 1am, it hurts!

c4n10
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250



View Profile
February 12, 2013, 11:51:02 AM
 #17

When I run:
Code:
INSERT INTO requests (req_type, uid, amount, curr_type) VALUES ('DEPOS', '169', '1', 'LTC');
from mysql command line it works.

but for some reason when the page runs that script it doesn't work...

echo your query statement in php. then copy it to mysql and run it, there may be some small errors, like not adding quotes around strings.

try hard coding the values as a test and  do
Code:
echo mysql_error();
right after your do_query

and stop coding at 1am, it hurts!

produces no errors... odd...
adamstgBit
Legendary
*
Offline Offline

Activity: 1904
Merit: 1002


Trusted Bitcoiner


View Profile WWW
February 12, 2013, 08:31:24 PM
 #18

When I run:
Code:
INSERT INTO requests (req_type, uid, amount, curr_type) VALUES ('DEPOS', '169', '1', 'LTC');
from mysql command line it works.

but for some reason when the page runs that script it doesn't work...

echo your query statement in php. then copy it to mysql and run it, there may be some small errors, like not adding quotes around strings.

try hard coding the values as a test and  do
Code:
echo mysql_error();
right after your do_query

and stop coding at 1am, it hurts!

produces no errors... odd...

so its working with hard coded values?

davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1002


1davout


View Profile WWW
February 12, 2013, 08:49:54 PM
 #19

Aah, the joys of PHP and echo'ing SQL statements carefully crafted by hand Cheesy

c4n10
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250



View Profile
February 14, 2013, 03:30:42 AM
 #20

Ok, that code is working perfectly now. It connects, queries and writes to the db exactly as it should (thanks everyone!!!).

Now, if anyone is interested, I am having a new problem.

Here is the problematic code:
Code:
<?php
require '../htdocs/config.php';
require 
'../util.php';

function 
update_req($reqid$status)
{
    
$query "
        UPDATE requests
        SET status='
$status'
        WHERE
            reqid='
$reqid'
            AND curr_type='RUC'
        "
;
    
do_query($query);
}

$query "
    SELECT
        requests.reqid AS reqid,
        uid,
        amount,
        addy
    FROM requests
    JOIN rucoin_requests
    ON requests.reqid=rucoin_requests.reqid
    WHERE
        req_type='WITHDR'
        AND amount > '0'
        AND status='VERIFY'
        AND curr_type='RUC'
    "
;
$result do_query($query);
$rucoin connect_rucoin();
while (
$row mysql_fetch_assoc($result)) {
    
$reqid $row['reqid'];
    
$uid $row['uid'];
    
$amount $row['amount'];
    
$addy $row['addy'];

    if (
gmp_cmp($rucoin->getbalance(""), $amount) >= '0')
    {
        
update_req($reqid"PROCES");
        
$rucoin->sendfrom(""$addy$amount);
        
update_req($reqid"FINAL");
    }
}

?>

This code should check the "requests" table for "req_type='WITHDR'" with amounts greater than '0' with "status='verify'" and "curr_type='RUC'" and when it finds a transaction it copies the "reqid" and "addy" into a table called "rucoin_requests". This part of the code works.

From there the script should connect to rucoin, check to make sure the default account has enough funds to send the requested withdrawal amount. Upon verification of funds it should connect to the table "requests" and update the status to "PROCES". After that it should connect to rucoin and "sendfrom" the default ("") account the requested "$amount" to the "$addy" listed in the "rucoin_requests" table.

Right now, the code seems to be stuck after it updates the status to "verify" and copies the "reqid" and "addy" into the "rucoin_requests" table...
Pages: [1] 2 »  All
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!