-1

I have no idea what this error means dose anyone know what it means

Fatal error: Call to a member function prepare() on a non-object in /home/u163876569/public_html/register.php on line 86

$checkUsername = $odb -> prepare("SELECT COUNT(*) FROM `users` WHERE `username` = :username");

Thats line 86

TylerH
  • 20,799
  • 66
  • 75
  • 101
user3627130
  • 1
  • 1
  • 1

2 Answers2

0

Connect to your DB first

$odb = new PDO("mysql:host=$my_db_host;dbname=$my_db_database", $my_db_username, $my_db_password);

if there is no error produced on this and you still get that Fatal error: Call to a member function prepare() on a non-object error, i can guess maybe something is wrong with the scope of $odb variable

Sharky
  • 6,154
  • 3
  • 39
  • 72
  • Why are you catching the exception? This is exactly the kind of thing that leads to the kind of error the OP is seeing. – deceze May 12 '14 at 05:23
  • @deceze whats wrong with that? im assuming 1) he is in development 2) he will see the echo – Sharky May 12 '14 at 08:19
  • If `new PDO` throws an exception, that means it could not connect to the database. If you can't connect to the database, there's pretty much no reason to execute any of the following code; you want your application to stop in that case. By catching the exception without `exit`ing, you're actually preventing the application from stopping as it should. Just simply don't catch the exception, which causes the error message to be displayed *and* the application to stop. – deceze May 12 '14 at 08:22
  • @deceze yup you are correct, i ll fix that code a bit – Sharky May 12 '14 at 08:24
  • Just. Simply. **Do. Not. Catch.** The. Exception. – deceze May 12 '14 at 08:24
  • @deceze wont the getMessage provide something usefull to dev? – Sharky May 12 '14 at 08:25
  • Have you ever seen an uncaught exception?! It displays the error message and much more! http://3v4l.org/LoggW#v510 You can customise what it should and shouldn't show in development vs. production through php.ini settings and/or custom exception handlers. – deceze May 12 '14 at 08:27
  • @deceze i love getting corrected, thanks :D – Sharky May 12 '14 at 08:29
0

check the object $odb because it is not created. try to print $odb:

var_dump($odb); die;

if give you error that means your $odb not created yet.

Mohammad Alabed
  • 809
  • 6
  • 17