Page 1 of 1

problem with a code

Posted: Sat Oct 02, 2010 5:08 pm
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?

Re: problem with a code

Posted: Sat Oct 02, 2010 6:36 pm
by califdon
You must put quotes around the index to the Rsql array:

Code: Select all

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

Re: problem with a code

Posted: Sat Oct 02, 2010 6:40 pm
by alvaro
also i put like this

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

still dont works

Re: problem with a code

Posted: Sat Oct 02, 2010 6:57 pm
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.

Re: problem with a code

Posted: Sat Oct 02, 2010 7:02 pm
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;