problem with a code

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

Post Reply
alvaro
Forum Newbie
Posts: 12
Joined: Sun Dec 21, 2008 4:12 pm

problem with a code

Post by alvaro »

hi guys, i have a code like this:

Code: Select all

<?php
function decode($string,$key) {
    $key = sha1($key);
    $strLen = strlen($string);
    $keyLen = strlen($key);
    for ($i = 0; $i < $strLen; $i+=2) {
        $ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
        if ($j == $keyLen) { $j = 0; }
        $ordKey = ord(substr($key,$j,1));
        $j++;
        $hash .= chr($ordStr - $ordKey);
    }
    return $hash;
}


$teste2 = decode("j4v59454i484t5r4r4","this is a key" );

echo "$teste2";
?>
this works perfect, my problem is the 2 strings inside $teste2=decode i want to get from database

i tried like this

Code: Select all

// $teste2 = decode("j4v59454i484t5r4r4","this is a key" );

$sql = mysql_query("SELECT * FROM data WHERE id='1'", $conexao);
$Rsql = mysql_fetch_array($sql);

$string1= $Rsql[chave1];
$string2= $Rsql[chave2];

$teste2 = decode($string1,$string2); //not works
$teste2 = decode($Rsql[chave1],$Rsql[chave2]); //not works

echo "$teste2";
how i can call the variables correct from database?
Last edited by califdon on Sat Oct 02, 2010 6:32 pm, edited 2 times in total.
Reason: Changed [code] to [syntax=php] tag for readability
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: problem with a code

Post by califdon »

You must put quotes around the index to the Rsql array:

Code: Select all

$string1= $Rsql['chave1'];
$string2= $Rsql['chave2'];
alvaro
Forum Newbie
Posts: 12
Joined: Sun Dec 21, 2008 4:12 pm

Re: problem with a code

Post by alvaro »

also i put like this

$string1= $Rsql['chave1'];
$string2= $Rsql['chave2'];

still dont works
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: problem with a code

Post by califdon »

Do you have a record in your table with id = '1'? Those quotes around the 1 make it a character string, not a numeric value--is that what you intended? Does that record have values in the fields "chave1" and "chave2"? In other words, is your query working? During script development and debugging, you should give MySQL a chance to help you by showing you any error messages. You do this by adding an or to the mysql command, like this:

Code: Select all

$sql = mysql_query("SELECT * FROM data WHERE id='1'", $conexao) or die(mysql_error());
Try that and see if your query is working.
alvaro
Forum Newbie
Posts: 12
Joined: Sun Dec 21, 2008 4:12 pm

Re: problem with a code

Post by alvaro »

My query (select * from .....)

return the results ok...

Code: Select all


$sql = mysql_query("SELECT * FROM data WHERE id='1'", $conexao);
$Rsql = mysql_fetch_array($sql);


$chave = $Rsql['chaveapi'];

$keygg = $Rsql['keyg'];

print $chave; // returns j4v59454i484t5r4r4    print $keygg; //returns   AAbb1245@!#$$AAbb%%&&

//$teste2 = decode($chave,$keygg); //not works

$teste2 = decode("j4v59454i484t5r4r4","AAbb1245@!#$$AAbb%%&&");

print $teste2;

Post Reply