0

I'm fairly new to PHP and I know this is a well known problem, however I wasn't able to fix it though. According to the mysql log the connection to the database is established and immediately closed.

I attached the code snipped below, thanks for any help. Btw, this error occurs only on the "real" server, using XAMPP I got no problems at all...

$stmt = $conn->prepare("SELECT UID FROM USERS WHERE username = ? and password = ?");
$stmt->bind_param("ss", $g_usernameSql, $g_pwSql);
MoBau
  • 9
  • 7
  • place `var_dump($stmt);` after first line, what do you see? – olegsv Jul 14 '16 at 12:38
  • try $rs= $stmt->bind_param("ss", $g_usernameSql, $g_pwSql);if ($rs) { $stmt ->execute();} – Dave Jul 14 '16 at 12:38
  • @olegsv it prints false – MoBau Jul 14 '16 at 12:40
  • view this , http://stackoverflow.com/questions/11374672/mysqli-bind-param-does-not-set-stmt-error-or-stmt-errno – Dave Jul 14 '16 at 12:42
  • You seem to have a problem with database connection. What is your code to connect with DB? Try to enable exception throwing after connection and see: ` $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` – olegsv Jul 14 '16 at 12:48

1 Answers1

0

Probable cause is: $conn->prepare is returning false because it is failing.

the $stmt is false and not a statement object. you should check if ($stmt === false) before using it.

Now to see why it failed check the array returned by $conn->errorInfo() (presuming $conn is a PDO instance)

$stmt = $conn->prepare("SELECT UID FROM USERS WHERE username = ? and password = ?");
if ($stmt === false) {
   var_dump($conn->errorInfo());
} else {
   // do stuff
}
svn
  • 1,235
  • 10
  • 22