Bitcoin Forum

Other => Off-topic => Topic started by: c4n10 on February 12, 2013, 04:17:03 AM



Title: Question for the php-heads...
Post by: c4n10 on February 12, 2013, 04:17:03 AM
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]


Title: Re: Question for the php-heads...
Post by: theymos on February 12, 2013, 04:48:47 AM
var_dump before gmp_cmp.

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


Title: Re: Question for the php-heads...
Post by: Herodes on February 12, 2013, 04:49:37 AM
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.




Title: Re: Question for the php-heads...
Post by: adamstgBit on February 12, 2013, 04:54:43 AM
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);


Title: Re: Question for the php-heads...
Post by: cosmicone on February 12, 2013, 04:58:30 AM
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);
    }
}


Title: Re: Question for the php-heads...
Post by: Herodes on February 12, 2013, 05:04:39 AM
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 (http://php.net/manual/en/function.gmp-cmp.php).

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.


Title: Re: Question for the php-heads...
Post by: theymos on February 12, 2013, 05:08:13 AM
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.


Title: Re: Question for the php-heads...
Post by: Herodes on February 12, 2013, 05:11:54 AM
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. :)

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 ?



Title: Re: Question for the php-heads...
Post by: adamstgBit on February 12, 2013, 05:30:48 AM
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. :)

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)


Title: Re: Question for the php-heads...
Post by: c4n10 on February 12, 2013, 05:51:45 AM
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...


Title: Re: Question for the php-heads...
Post by: Herodes on February 12, 2013, 06:12:30 AM
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.


Title: Re: Question for the php-heads...
Post by: adamstgBit on February 12, 2013, 06:23:00 AM
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   :P
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?


Title: Re: Question for the php-heads...
Post by: c4n10 on February 12, 2013, 06:31:26 AM
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...


Title: Re: Question for the php-heads...
Post by: c4n10 on February 12, 2013, 06:38:18 AM
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   :P
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...


Title: Re: Question for the php-heads...
Post by: Herodes on February 12, 2013, 06:44:50 AM
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.


Title: Re: Question for the php-heads...
Post by: adamstgBit on February 12, 2013, 06:52:37 AM
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!


Title: Re: Question for the php-heads...
Post by: c4n10 on February 12, 2013, 11:51:02 AM
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...


Title: Re: Question for the php-heads...
Post by: adamstgBit on February 12, 2013, 08:31:24 PM
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?


Title: Re: Question for the php-heads...
Post by: davout on February 12, 2013, 08:49:54 PM
Aah, the joys of PHP and echo'ing SQL statements carefully crafted by hand :D


Title: Re: Question for the php-heads...
Post by: c4n10 on February 14, 2013, 03:30:42 AM
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...


Title: Re: Question for the php-heads...
Post by: payb.tc on February 14, 2013, 03:41:28 AM
Aah, the joys of PHP and echo'ing SQL statements carefully crafted by hand :D

yeah these days i'm more likely to be using codeigniter's $db->last_query() function after having it constructed for me :D


Title: Re: Question for the php-heads...
Post by: Herodes on February 14, 2013, 03:43:29 AM
Not sure what kind of tools you use to develop, but I just set up vim with xdebug on my personal development machine, and it works wonders:
https://bitcointalk.org/index.php?topic=143923.0

I had an issue where some queries to get the current bitcoin price didn't work. So the usual method would be to make echo statements, and see where things went wrong, but then I installed this, and now I can step into functions, and execute code line by line, yay. I found the error I was chasing immediately, instead of using 30 minutes with echo statements. :)


Title: Re: Question for the php-heads...
Post by: c4n10 on February 14, 2013, 03:48:08 AM
Not sure what kind of tools you use to develop, but I just set up vim with xdebug on my personal development machine, and it works wonders:
https://bitcointalk.org/index.php?topic=143923.0

I had an issue where some queries to get the current bitcoin price didn't work. So the usual method would be to make echo statements, and see where things went wrong, but then I installed this, and now I can step into functions, and execute code line by line, yay. I found the error I was chasing immediately, instead of using 30 minutes with echo statements. :)

I've never really used vim much, wasn't aware of the xdebug function, I always just code/edit in nano but sounds like vim might be worth taking a look at...


Title: Re: Question for the php-heads...
Post by: Herodes on February 14, 2013, 04:07:37 AM
Not sure what kind of tools you use to develop, but I just set up vim with xdebug on my personal development machine, and it works wonders:
https://bitcointalk.org/index.php?topic=143923.0

I had an issue where some queries to get the current bitcoin price didn't work. So the usual method would be to make echo statements, and see where things went wrong, but then I installed this, and now I can step into functions, and execute code line by line, yay. I found the error I was chasing immediately, instead of using 30 minutes with echo statements. :)

I've never really used vim much, wasn't aware of the xdebug function, I always just code/edit in nano but sounds like vim might be worth taking a look at...

Vim has a very steep learning code, but you can do all kinds of awsome things with it, and that's not just for the showoff factor, but also for the practical factor. The cool thing is that there's no menus, no nothing on your screen, so people looking at you will think you're some kind of wizzard. Keeping a cheat sheet near the computer or on the computer is a nice thing. I am sure there's a lot of beginners guides. There's a lot of nice shortcuts that let you perform magic. You can do block editing, ie. vertical editing, you can make macros etc. and there exist a lot of plugins, with ctags you can even get autocomplete. So for the long run it rocks. Once you start mastering it, you will never go back.



Title: Re: Question for the php-heads...
Post by: payb.tc on February 14, 2013, 04:39:54 AM
not that netbeans is perfect, but you can use xdebug in that no problem, you don't have to use vim


Title: Re: Question for the php-heads...
Post by: c4n10 on February 14, 2013, 07:08:18 AM
Alright, I have this 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  (
$rucoin->getbalance('') >= 0)
    {
        
update_req($reqid'PROCES');
        
$rucoin->sendfrom(''$addy$amount);
        
update_req($reqid'FINAL');
    }
}
?>

And it is working perfectly all the way up to:

Code:
$rucoin->sendfrom('', $addy, $amount);

I believe I have a syntax error being fed to the jsonRPCClient as I know the $addy and $amount are both returning correct values, I have tested issuing the command by hand to rucoind so I know it supports the "sendfrom" function and is accessible yet I am receiving an HTTP 500 error from rucoind.

I am using the "$rucoin = connect_rucoin" function in various places to send calls to rucoind and retrieve information, all without error, but this last thing just does not seem to want to work.

Anyone have any ideas...?


Title: Re: Question for the php-heads...
Post by: payb.tc on February 14, 2013, 09:35:04 AM
Alright, I have this 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  (
$rucoin->getbalance('') >= 0)
    {
        
update_req($reqid'PROCES');
        
$rucoin->sendfrom(''$addy$amount);
        
update_req($reqid'FINAL');
    }
}
?>

And it is working perfectly all the way up to:

Code:
$rucoin->sendfrom('', $addy, $amount);

I believe I have a syntax error being fed to the jsonRPCClient as I know the $addy and $amount are both returning correct values, I have tested issuing the command by hand to rucoind so I know it supports the "sendfrom" function and is accessible yet I am receiving an HTTP 500 error from rucoind.

I am using the "$rucoin = connect_rucoin" function in various places to send calls to rucoind and retrieve information, all without error, but this last thing just does not seem to want to work.

Anyone have any ideas...?

PHP can sometimes do weird things with variable types... try this and get back to us:

Code:
$rucoin->sendfrom('', $addy, (float) $amount);


Title: Re: Question for the php-heads...
Post by: c4n10 on February 14, 2013, 12:40:30 PM
PHP can sometimes do weird things with variable types... try this and get back to us:

Code:
$rucoin->sendfrom('', $addy, (float) $amount);

Hmmm, I will have to give that a try sometime, was actually coming back to report that I got it working with:

Code:
$rucoin->sendfrom('', $addy, (double)$amount);


Title: Re: Question for the php-heads...
Post by: payb.tc on February 14, 2013, 12:56:04 PM
PHP can sometimes do weird things with variable types... try this and get back to us:

Code:
$rucoin->sendfrom('', $addy, (float) $amount);

Hmmm, I will have to give that a try sometime, was actually coming back to report that I got it working with:

Code:
$rucoin->sendfrom('', $addy, (double)$amount);

just as good