encrypt/decrypt a field - php/mysql

Posted on 2006-06-06
Last Modified: 2008-01-09
I'm testing out a DB for a friend. He's got a field with a field being encrypted using the following:


When I try to decode the string from the DB, the following doesn’t work....


I've been searching online for a bit, and the closest thing that I've found is: DECODE(crypt_str,pass_str)   as outlined on

But I'm not sure where to get the pass_str from.
Question by:webmanager

    Author Comment

    a few minor updates....

    I'm now (for testing purposes) using:


    When I try to decode the string from the DB, the following doesn’t work....


    The error I get on the webpage that has the decode script is:
    Fatal error: Call to undefined function: decode() in test.php on line 24
    LVL 33

    Expert Comment

    I think the encode and decode are MySQL functions, not PHP functions.  So you'd want something like this:
    $sql = "SELECT DECODE('{$row['numberPass']}', 'someValue') AS password";
    $result = mysql_query($sql) or die(mysql_error());

    pass_str will be built in to whatever code encodes and stores passwords.

    Author Comment

    I guess I should have said this before... lol

    I have an index.php page with a single field form called numberPass.
    The form on index.php submits to submit.php

    On submit.php, this is what I have...

    $query = "INSERT INTO tablename ( numberPass ) VALUES ( ENCODE('$_POST[numberPass]','123') )";
    <h3>Thank you for your entry.</h3>

    on the page that I want to view the encrypted info (it's called view.php), this is what I have.

    <title>View info</title>
    $query = "SELECT numberPass FROM tablename";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result))
    <li><?php print $row[numberPass]; ?></li>
    <li><?php DECODE("$row[numberPass]","123"); ?></li>

    LVL 33

    Accepted Solution

    As I said, the DECODE function is a MySQL function, not a PHP function.  So that view.php should contain a query like
    $query = "SELECT numberPass, DECODE(numberPass, '123') AS decoded FROM tablename";

    and further on down,
    <?php print $row['numberPass']; ?>
    <?php print $row['decoded']; ?>

    Author Comment

    Works like a charm...  


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now