PHP Help Needed - Noobie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

aodat2
Forum Newbie
Posts: 12
Joined: Wed Aug 13, 2008 7:19 am

PHP Help Needed - Noobie

Post by aodat2 »

I was given a code which I am suppose to incorporate into my website.

Can someone be kind enough to help me by telling me what I should do here to make the code work? I do understand that in the end of the Function, it will return a value or something like that which is in the variable $Result but can someone tell me what else I am suppose to do to get the Function working or how am I suppose to call the Function?

Thanks a lot for the help in advance.

Code: Select all

<?PHP
function Requery(){
$query = "http://www.yourdomain.com/?MerchantCode=" .
$MerchantCode . "&RefNo=" . $RefNo . "&Amount=" . $Amount;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout);
if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = split("\n", $buf);
$Result = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
return $Result;
}
?>
User avatar
lukewilkins
Forum Commoner
Posts: 55
Joined: Tue Aug 12, 2008 2:42 pm

Re: PHP Help Needed - Noobie

Post by lukewilkins »

Code: Select all

$myresult = Requery();
// $myresult now holds the return from $Result inside the function
echo $myresult;
So your full code should look like:

Code: Select all

<?PHP
function Requery(){
$query = "http://www.yourdomain.com/?MerchantCode=" .
$MerchantCode . "&RefNo=" . $RefNo . "&Amount=" . $Amount;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout);
if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = split("\n", $buf);
$Result = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
return $Result;
}
 
$myresult = Requery();
// $myresult now holds the return from $Result inside the function
echo $myresult;
 
?>
Hope that helps.

Luke
aodat2
Forum Newbie
Posts: 12
Joined: Wed Aug 13, 2008 7:19 am

Re: PHP Help Needed - Noobie

Post by aodat2 »

Thanks thanks for the help :)

I think I have that part down.

The only problem I have now is what's left unknown. I have the variables $MerchantCode and $RefNo but I do not have the amount.

Could you tell me how I could obtain a value from a MySQL table?

Example:
Payment_Table contains the fields Name, Address, Amount, RefNo and Transaction_ID.

How could I get the variable from the MySQL table out so that I could use it?

Thanks for the help in advance.
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: PHP Help Needed - Noobie

Post by desmi »

You really should check out some basics about mysql querys, google is a good friend :)
aodat2
Forum Newbie
Posts: 12
Joined: Wed Aug 13, 2008 7:19 am

Re: PHP Help Needed - Noobie

Post by aodat2 »

I have tried googling but the examples there are either extremely complex or I don't really know what to look for. Do you mind helping by giving a few pointers on what I should google for or etc?

Thanks again!
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: PHP Help Needed - Noobie

Post by desmi »

http://www.tizag.com/mysqlTutorial/mysqlselect.php

Here you can find other than SELECT tutorials too, and i find those really easy and explained tutorials.
(Oh and btw, i just googled 'mysql tutorial')
aodat2
Forum Newbie
Posts: 12
Joined: Wed Aug 13, 2008 7:19 am

Re: PHP Help Needed - Noobie

Post by aodat2 »

Thanks a lot.

Read it already... have a fast question. Is my statement correct?

Code: Select all

$query3 = "SELECT Amount FROM payment WHERE RefNo = '$RefNo'";
Of coz the $RefNo is a variable like above.
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Re: PHP Help Needed - Noobie

Post by idevlin »

Try

Code: Select all

$query3 = "SELECT Amount FROM payment WHERE RefNo = '" . $RefNo . "'";
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: PHP Help Needed - Noobie

Post by desmi »

idevlin's code is correct, and yours is too, if its inside mysql_query()

There are many ways to do it:

Code: Select all

 
$query3 = "SELECT Amount FROM payment WHERE RefNo = '" . $RefNo . "'";
$r = mysql_query($query3);
 
or

Code: Select all

 
$query = mysql_query("SELECT Amount FROM payment WHERE RefNo = '$RefNo'");
 
aodat2
Forum Newbie
Posts: 12
Joined: Wed Aug 13, 2008 7:19 am

Re: PHP Help Needed - Noobie

Post by aodat2 »

Don't really see where I have this wrong... but it seems to be giving me "Invalid Parameter" as the result.

Can you guys tell me where I got it wrong? The funny thing is that when I enter it directly into the browser, it will work correctly. Just not over here. Hope you guys can help point out where my mistake is.

Code: Select all

 
$MerchantCode = "12345";
$RefNo = "22";
$Amount_Paid = "1.00";
 
function Requery(){
$query = "http://www.yourdomain.com/enquiry.asp?MerchantCode=" .
$MerchantCode . "&RefNo=" . $RefNo . "&Amount=" . $Amount_Paid;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout);
if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$buf .= fgets($fp, 128);
}
$lines = split("\n", $buf);
$Result = $lines[count($lines)-1];
fclose($fp);
} else {
# enter error handing code here
}
return $Result;
}
 
$verify = Requery();
echo $verify;
 
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: PHP Help Needed - Noobie

Post by desmi »

You have changed this line, havn't you ?:

Code: Select all

$query = "http://www.yourdomain.com/enquiry.asp?MerchantCode=" .
aodat2
Forum Newbie
Posts: 12
Joined: Wed Aug 13, 2008 7:19 am

Re: PHP Help Needed - Noobie

Post by aodat2 »

The original Tech Notes says it's like that (with the "enquiry"). The one above I accidentally deleted it out. Sorry about the confusion.
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: PHP Help Needed - Noobie

Post by desmi »

Umm, what i ment was, did you change that "yourdomain" to your own domain?
aodat2
Forum Newbie
Posts: 12
Joined: Wed Aug 13, 2008 7:19 am

Re: PHP Help Needed - Noobie

Post by aodat2 »

Yes of coz I did. I have changed that and yet it still says "Invalid Parameters". I'm not too sure what Parameters are wrong.
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: PHP Help Needed - Noobie

Post by desmi »

Could you post the whole page as it is, with proper domain-name and all.. It would be easier to figure out the problem :)
Post Reply