Links: dibi | API reference

Forum: [česky] [english]

dibi forum

tiny ‘n’ smart
database layer

You are not logged in.

#1 3 years ago

gotisch
Member
Registered: 2009-05-23
Posts: 3

SQL Function in query

Hello,

how can i add a SQL function that takes parameters in the query?

<?php
function CreateUser($username, $email, $password)
{
        $connection = dibi::getConnection('Server');
        $insert = array(
                'username' => strtoupper($username),
                'pass' => "SHA1('" . strtoupper($username) . ":" . strtoupper($password) . "')",
                'email' => $email,
        );
        $connection->query("INSERT INTO users ", $insert);
        return;
}
CreateUser("test", "test@hotmail.com", "mypass123'%=,.");
?>

The problem is with the pass. If i just add the string it will esacpe the SHA1(' part too. SHA(\\' so in the end i end up with loads of \\ and the query not working.

if i do

<?php
new DibiVariable("SHA1('" . strtoupper($username) . ":" . strtoupper($password) . "')", "sql"),
?>

instead, it will not get escaped. And i can't use $connection->getDriver()->escape($username,dibi::TEXT) because that require a connection to the server already established, and i want the connection setting lazy = true

Thanks for any help!

 

#2 3 years ago

David Grudl
Administrator
Registered: 2005-02-08
Posts: 5833

Re: SQL Function in query

You can use this:

$insert = array(
        'username' => strtoupper($username),
        'pass%sql' => array('SHA1(%s)',  strtoupper("$username:$password")), // or only 'pass' => in last revision
        'email' => $email,
);

 

Board footer