1

I have an earlier post here But NONE of those answers worked. So here's my entire class code:

<?php
session_start();
class Mysql {
    private $conn;

    function __construct() {
        $this->conn =  new PDO('mysql:host=***;dbname=***;charset=UTF-8','***','***') or 
                      die('There was a problem connecting to the database.');
    }

    function verify_Username_and_Pass($un, $pwd) {
        $query = "SELECT Username
                FROM Conference
                WHERE Username = :un AND Password = :pwd";

        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(':un', $un);
        $stmt->bindParam(':pwd', $pwd);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            // User exist
            $stmt->bindColumn('First Name', $firstName); 
            $_SESSION["FirstName"] = $firstName; 
            die($_SESSION["FirstName"]);
            return true;
            $stmt->close();
        }
        else {
            // User doesn't exist
            //die("failure");
            return false;
            $stmt->close();
        }
    }
}
?>

I've tried fetch, i've tried bind_result, etc and none of them print the correct value on the die statement. Now this worked when i stored username in session and tried to print that. What is wrong with the code?

2
  • What is the error? What does not work? And what does the die($_SESSION["FirstName"]); ? Commented Oct 2, 2012 at 18:53
  • @JvdBerg it literally prints nothing. But if i stored username then it prints the correct username. I've even tried the bindcolumn with back ticks and it still didn't retrieve the correct first name. Commented Oct 2, 2012 at 18:54

2 Answers 2

1
  1. You aren't calling the code in the snippet. What's the call procedure?

  2. You aren't retrieving Conference.First Name only Conference.Username so you should be getting a warning unless you're not displaying errors. You probably want

"SELECT * FROM Conference...."

or

"SELECT FirstName From Conference WHERE Username = :un AND Password = :pwd";

  1. It's possibly Conference.FirstName.

  2. die(); is not very useful for debugging. Try var_dump($_SESSION["FirstName"]); die();

Sign up to request clarification or add additional context in comments.

Comments

1

I have looked at your code, and made a working version on my own server. If you have any questions, feel free to ask.

class Mysql
{
  private $conn;
  public  $error;
  public  $username;

  function __construct()
  {
    try {
      $this->conn = new PDO( 'mysql:host=localhost;dbname=****', 'root', '****' );
      $this->conn->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

    }
    catch ( Exception $e ) {
       $this->error = $e->getMessage();
    }
  }

  function verify_Username_and_Pass( $un, $pwd )
  {
    $query = "SELECT Username
              FROM Conference
              WHERE Username = :un AND Password = :pwd";

    $stmt = $this->conn->prepare( $query );
    if( !$stmt ) {
      $this->error = $this->conn->errorInfo();
      return false;        
    }

    $stmt->bindParam( ':un', $un );
    $stmt->bindParam( ':pwd', $pwd );
    $stmt->execute();
    if ( $stmt->rowCount() > 0 ) {
      // User exist
      $this->username = $stmt->fetchColumn();
      return true;
    } 
    else {
      // User doesn't exist
      return false;
    }
  }

}

session_start();

$db = new Mysql();
if( !$db->error ) {
  if( $db->verify_Username_and_Pass ( 'user', 'test' )) {
    $_SESSION["FirstName"] = $db->username;
  }
  else
    echo 'Unknown user';
}

var_dump( $db );

The script will output this:

Unknown user

object(Mysql)#1 (3) { 
["conn":"Mysql":private]=> object(PDO)#2 (0) { } 
["error"]=> array(3) 
   { [0]=> string(5) "42S02" 
     [1]=> int(1146) 
     [2]=> string(39) "Table   'xxxx.Conference' doesn't exist" } 
["username"]=> NULL }

5 Comments

Seeing as you are calling a variable you never set $db->firstName is this really working??
@PhilipWhitehouse: thanks for pointing that out. I did not test with a existing user. Corrected and updated the code.
no worries, wrote my own list of fixes based on his code. Hopefully there's enough to go on :-)
@JvdBerg fistname is not the same as username, if it was i wouldn't have posted a question considering i already have their username (it's what they typed in)
@CharlieYabben: but even in that case you would have got error(except the case if you have disabled error reporting)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.