Tuesday, November 27, 2007

Don't Give Space When Calling MySQL Function

Dear people ... this might be a simple problem but you can take a whole day to trace where the problem exist if you didn't care about this. I face this problem and get confuse when working with MySQL.

I have the following code and execute well using phpMyAdmin but it's failed (no result set given) when i try to execute directly from my php script.

$strSql = "
SELECT user_password, user_name
FROM user_t
WHERE 1
AND user_id = '" . $_POST['txt_id'] . "'
AND user_password = SHA1 ('" . $_POST['txt_pwd'] . "')" ;

if ( $objRS = mysql_query ($strSql, $objConn) )
{
// Do something here ...
}

As you can see in the bold style of the code above we can see that there is a space between MySQL SHA1 function and the parameter for that function.

After i remove that space then the problem is solved. So, my code now looks like below :

$strSql = "
SELECT user_password, user_name
FROM user_t
WHERE 1
AND user_id = '" . $_POST['txt_id'] . "'
AND user_password = SHA1('" . $_POST['txt_pwd'] . "')" ;

if ( $objRS = mysql_query ($strSql, $objConn) )
{
// Do something here ...
}

So, keep your MySQL function closed with ( without space after function name. Hope this helps.

No comments: