3 Replies - 676 Views - Last Post: 02 February 2013 - 04:23 AM Rate Topic: -----

#1 jeansymolanza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 20-February 08

Fatal error: Call to a member function prepare() line 22

Posted 01 February 2013 - 05:39 PM

Not sure why I keep getting these errors all of a sudden...

Fatal error: Call to a member function prepare() on a non-object in login.php on 
line 35 Call Stack: 0.0005 675088 1. {main}() login.php:0 0.0017 678544 2.
LoginSubmit() login.php:21


Is it because of the way I'm handling my database connection?

Btw don't mind the fact that passwords are being stored as simple text because the system being built is for demo purposes only.

// database connection

$hostname = "xxx";
$username = "xxx";
$dbname = "xxx";
$password = "xxx";

try {
    $pdo = new PDO("mysql:host=$hostname;dbname=$dbname", 
    $username, $password);
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

// login code     

if(isset($_POST['pmsubmit']))
{
  LoginSubmit('pm', 'pmname', 'pmpass');
}

if(isset($_POST['tssubmit']))
{
  LoginSubmit('ts', 'dept', 'tspass');
}

function LoginSubmit($pm_or_ts, $the_name, $the_pass)
{
  $the_name = $the_name;
  $posted_name = $_POST[$posted_name];
  $posted_pass = $_POST[$posted_pass];
  // check if password matches the one in the table
  $query = $pdo->prepare("SELECT * FROM db_pass WHERE pass = :pass"); // line 21
  $query->execute(array(":pass" => $posted_pass));
  // if there is a match then we log in the user
  if ($query->rowCount() > 0)
  {
    // session stuff
    $_SESSION[$the_name] = $posted_name;
    // refresh page
    header( 'Location: ' . $pm_or_ts . '/index.php' ) ;
  } 
  // if there is no match then we present the user with an error
  else
  {
    echo "error";
    exit;
  }
}

This post has been edited by jeansymolanza: 01 February 2013 - 05:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Fatal error: Call to a member function prepare() line 22

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3492
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: Fatal error: Call to a member function prepare() line 22

Posted 01 February 2013 - 05:45 PM

you don’t pass the $pdo variable to the function, hence $pdo is null and that throws the error you encounter.

and if you only are interested in the row count, use SQL’s COUNT() function and do not fetch data you don’t use.

further, always exit after a redirect header. though at best you don’t even use that and insert the appropriate code directly.

problem #2: the try…catch() only covers the PDO instantiation and not the whole PDO code. hence if the instantiation fails, you echo the message and continue as if the connection would have been successful.
Was This Post Helpful? 1
  • +
  • -

#3 jeansymolanza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 20-February 08

Re: Fatal error: Call to a member function prepare() line 22

Posted 01 February 2013 - 06:10 PM

View PostDormilich, on 01 February 2013 - 05:45 PM, said:

you don’t pass the $pdo variable to the function, hence $pdo is null and that throws the error you encounter.

and if you only are interested in the row count, use SQL’s COUNT() function and do not fetch data you don’t use.
further, always exit after a redirect header. though at best you don’t even use that and insert the appropriate code directly.


I managed to resolve this by doing the following:

function LoginSubmit($pm_or_ts, $the_name, $the_pass)
{
    global $pdo;
    ..
    ..
}


View PostDormilich, on 01 February 2013 - 05:45 PM, said:

problem #2: the try…catch() only covers the PDO instantiation and not the whole PDO code. hence if the instantiation fails, you echo the message and continue as if the connection would have been successful.


Not sure how to go about doing this? Can you show me in my code please?

try {
    $pdo = new PDO("mysql:host=$hostname;dbname=$dbname", 
	$username, $password);
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3492
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: Fatal error: Call to a member function prepare() line 22

Posted 02 February 2013 - 04:23 AM

View Postjeansymolanza, on 02 February 2013 - 02:10 AM, said:

I managed to resolve this by doing the following:

function LoginSubmit($pm_or_ts, $the_name, $the_pass)
{
    global $pdo;
    ..
    ..
}

though a solution, this is the worst solution you could choose. globals are always a nightmare to debug. passing around a variable is much more straightforward.
function LoginSubmit($pdo, $pm_or_ts, $the_name, $the_pass)
{
    ..
    ..
}



View Postjeansymolanza, on 02 February 2013 - 02:10 AM, said:

Not sure how to go about doing this? Can you show me in my code please?

wrap your DB related code into a try…catch(). in your OP the try{} would span lines #9 to #28.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1