brabus85
brabus85

Reputation: 71

get value from mysql row pdo

Hi I am a beginner at pdo and try to get a variable from the column code and then check if the variable is has the same value how the inputelement code.

Thats the error message:

Fatal error: Call to a member function fetchColumn() on string in C:\xampp\htdocs\social\activation1.php on line 23

<?php require_once './auth.php'; ?>
<?php
if(isset($_POST["submit"])){

    $hostname='localhost';
    $user='root';
    $password='';

    if (isset($_POST['code'])) {
        $code=$_POST['code'];
    }

    $username = ($_SESSION['user']['username']);

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=loginsystem",$user,$password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
        $sql = " SELECT code FROM user2 WHERE username = '$username'";
        $result = $sql->fetchColumn();
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    if ($result == $_POST['code']) {
        $message['success'] = 'Neuer Benutzer (' . htmlspecialchars($_POST['username']) . ') wurde angelegt, <a href="login.php">weiter zur Anmeldung</a>.';
        header('Location: http://' . $_SERVER['HTTP_HOST'] . '/social/main.php');
    } else {
        print('<pre> Please try it again. :: ');
        print('</pre>');
    }
}
?>

Upvotes: 2

Views: 74

Answers (1)

Saty
Saty

Reputation: 22532

I think you forger query statement $dbh->query().Also username is written in quotes

 $sql = "SELECT code FROM user2 WHERE username = '".$username."' ";
  if ($res = $dbh->query($sql)) {// need to add this line in your code
      // then after fetchColumn
     $result = $res->fetchColumn();
   }

Upvotes: 1

Related Questions