Jump to content

mysql pdo not throwing exceptions while connecting except "object(PDO)#1 (0) { }"


shan2batman

Recommended Posts

hi guys i recently deployed an app in heroku through ubuntu. After troubleshooting it multiple times i finally got to install and retrieve data in clearDB mysql server in heroku with mysql workbench. But the problem is after deploying the php code, i get the following message

 object(PDO)#1 (0) { }

when i var_dump the PDO connection, here is the code to the dbconfig file that has the connection details.

    $url=parse_url(getenv("CLEARDB_DATABASE_URL"));
   
      $server = $url["host"];
      $username = $url["user"];
      $password = $url["pass"];
      $db = substr($url["path"],1);
      $host= "mysql:host=$server;dbname=$db";
      var_dump($host);
    try {
        $conn=new PDO($host , $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   
    } catch (PDOException $exc) {
        echo $exc->getMessage();
    }
    var_dump($conn);
    echo "<br>";
    var_dump($exc);
    include 'classes.inc.php';
   
     echo "<br>".$db."<br>";
    $project= new projecteg($conn);

please help me with this error as i'm running like nowhere with this problem.

Link to comment
Share on other sites

since the script was not pulling data i finally used exit to check connection and it was giving that to pdo, normally it should atleast give the statement when we use var_dump right but it was not showing the connection credentials either here is the login script so far while troubleshooting.

<?php  
include_once 'dbconfig.inc.php';
if (isset($_POST['submit-login'])) {
$uname= htmlspecialchars($_POST['unamel']);
$unamel=  stripslashes($_POST['unamel']);
$pass= htmlspecialchars($_POST['passl']);
$pass1= stripslashes($_POST['passl']);
$passl=  md5($pass1);
$user = $project->viewProtectedArea($unamel,$passl);var_dump($user);
exit();
if ($user!="") {
   
                 $_SESSION['id']=$user['user_id'];
                  $_SESSION['fname']=$user['fname'];
                   $_SESSION['lname']=$user['lname'];
                  $_SESSION['uname']=$user['uname'];
                  $_SESSION['email']=$user['email'];
                  $_SESSION['phone']=$user['phone'];
                  $_SESSION['avatar']=$user['avatar'];
                  $_SESSION['app']=TRUE;
                  $user_ok=TRUE;
                  if (isset($_SESSION[u2])) {
                      header("location: ../home.php?u={$_SESSION['u2']}&v={$_SESSION['uname']}");
                  } else {
                       header("location: ../home.php?u={$_SESSION['uname']}");
                  }
                
   
}}
if (isset($_SESSION['app'])&&$_SESSION['uname']!="") {
    //echo 'your name is '.$_SESSION['fname'];  
    header("location: ../home.php?u={$_SESSION['uname']}");
                  } else {
                      header("location: ../index.php?usernotfound?id=017");
                  }
Link to comment
Share on other sites

plus the class is here

public function viewProtectedArea($unamel,$passl)
                {
                 // $active=1;
                   $stmth= $this->_db->prepare("select * from user where uname=:uname and pass=:pass and activated=1");
                   $stmth->bindValue(":uname",$unamel);
                 $stmth->bindValue(":pass",$passl);
                   $stmth->execute();
                  return $stmth->fetchAll(PDO::FETCH_ASSOC);
              
              
           }
Link to comment
Share on other sites

if you are not getting any output from var_dump($user), then either if (isset($_POST['submit-login'])) { is false and none of the code is running, or one of the pdo statements is throwing an error/exception, which php will catch for you, based on the posted code, and use it's error_reporting/display_errors/log_errors settings to determine what happens with the actual error information. 

 

what exactly are your error_reporting/display_errors/log_errors settings? if they are not set to either report and display all errors or report and log all errors, nothing will happen with the error information and since you have already output something to the browser, there won't be a http 500 error either. error_reporting should always be set to E_ALL (or a -1) and you should have display_errors set to ON when developing and debugging code, and display_errors set to OFF and log_errors set to ON when running code on a live/public server.

 

next, this code won't work as written, you are using ->fetchAll() to retrieve the data, so you will either be getting an empty array if the query didn't match a row, or an array of row(s) if it did. You would either need to reference the zero'th element of the array to get the row of data, or you would need to just use ->fetch(), since the query should at most match a single row.

 

you should also not use md5() for the password hash. see php's password_hash() and password_verify() functions.

 

htmlspecialchars() is an output function. it is used when you output data in a html context. it is not used on data being supplied as input to an sql query. you are also not actually using the two variables that are being set in the lines using htmlspecialchars().

 

the problem that required the use of stripslashes() on data was removed from php several years ago. there's currently no need to apply stripslashes() to data.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.