if i put the code in my theme’s function.php file can it work?

Short Answer YES!! why because all a functions files does is act as a big huge plugin. Keep in mind though if you plan on using this with other themes or if in network mode you need the plugin file

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)