CodeIgniter Forums
Using the database driver within a library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using the database driver within a library (/showthread.php?tid=45555)



Using the database driver within a library - El Forum - 09-26-2011

[eluser]thisischris[/eluser]
Hey guys, I'm pretty new to CG and have stumbled into my first bump.
I'm trying to use the database within a custom library.

I can only guess that it's because it's not extending the CI_Controller hence not available? What should I be doing?

I get the following error:
Fatal error: Call to a member function database() on a non-object in...

Which is:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User {
    public function check_user_avail($string)
    {
  $this->load->database();
  $query = $this->db->query("SELECT COUNT(*) FROM users WHERE username='$string'");
  if ($query->num_rows() == 0)
  {return TRUE;
  }
  else
  {return FALSE;
  }
    }
}
/* End of file User.php */