0

Im attempting to rewrite my code with PDO. It is hosted on a commercial unix shared hosting (php 5,4, mysql pdo driver is available).

The code:

class User {
//the basic properties of the User
public $uid; // = login
public $password; // password

//db creds
const server = 'db14.somedomain.com';
const db = "db_name";
const mysql_login = "mysql_login";
const mysql_pass = "somepass";

// prepared statements

//main constructor
public function __construct ($uid, $password){
    $this->uid = $uid;
    $this->password = md5($password);

    }

public function login (){
    // defining the constants below is redundant, i know.

    define ("DB_DRIVER",  "mysql");
    define ("DB_CHARSET", "UTF8");
    define ("DB_HOST",    "$this->server");
    define ("DB_USER",    "$this->mysql_login");
    define ("DB_PASS",    "$this->mysql_pass");
    define ("DB_NAME",    "$this->mysql_login");

    try {
        $conn = new PDO("dbname=".DB_NAME.";mysql:host=".DB_HOST, DB_USER, DB_PASS);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // prepare sql and bind parameters
        $stmt = $conn->prepare ("SELECT uid, fname, lname, dob, suspended, language FROM users WHERE users.'uid'=:uid AND users.'password'=:password LIMIT 1");
        $stmt->bindParam("uid", $this->uid);
        $stmt->bindParam("password", $this->password);
        $stmt->execute();
        $count=$stmt->rowCount();
        $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
        $conn = null;

        if($count)
            {
            $_SESSION['uid']=$data->uid; // Storing user session value
            return true;
            }
            else
            {
            return false;
            } 
    }
            catch(PDOException $e) {
            echo 'error: '. $e->getMessage() ;
            }

}
}

$user = new User ($_POST['login'], $_POST['password']);
$login_check = $user->login();

The script returns error "could not find driver"

Could anyone please look into this?

RWS
  • 538
  • 5
  • 14
  • 2
    http://php.net/manual/en/ref.pdo-mysql.connection.php – Kevin Aug 25 '16 at 06:44
  • Thank you. Initially i fed the db and host in the documented order. If i do like this: $conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS); I get SQLSTATE[HY000] [2002] No such file or directory – RWS Aug 25 '16 at 06:51
  • 1
    http://php.net/manual/en/language.oop5.constants.php – Kevin Aug 25 '16 at 06:57
  • This is NOT a duplicate question, since the PDO driver is installed and available! – RWS Aug 25 '16 at 07:21
  • 2
    Yes, my bad, I was fooled by the title that says that your code doesn't work on a hosting while it turns out it wouldn't work anywhere due to wrong syntax. However, it makes this question off topic, as you are supposed to be able to follow basic PHP syntax guidelines. Please avoid using constants for a while and write all the credentials right in place, without constants or variables. – Your Common Sense Aug 25 '16 at 07:26
  • Switching to variables worked, please make a reply to this question and I will mark it as correct. – RWS Aug 25 '16 at 07:52

0 Answers0