PHP MySQL password hashing returns always false
<p>I know this question is asked so many times and there are many tutorials about this thematic but I can't explain what I'm doing wrong. Could you pls tell me what is wrong in my code? The database connection works as it should, but I don't know where the problem is or if I understood something wrong, when I don't hash the password and entry it directly there is no problem. No matter what I entry my login will not work and always returns "false".<br><br>Here is my code (note, I have a custom DBConnector class that handles variable types)</p>

<pre><code>    public function insertUser(){
        if (isset($_POST['name']) &amp;&amp; isset($_POST['password'])){
            try
            {
                $name = $_POST['name'];
                $password = $_POST['password'];

                $db = CDatabase::getInstance();
                //check if username is already in use
                $db-&gt;prepare("SELECT * FROM user where LOWER(name) = LOWER(?)");
                $db-&gt;bindParams(array($name));
                $db-&gt;execute();

                $result = $db-&gt;fetch();
                //if no user with the same name was found
                if ($result-&gt;num_rows == 0){
                    //insert user without password
                    $db-&gt;prepare("INSERT INTO user (name) VALUES(?)");
                    $db-&gt;bindParams(array($name));
                    $db-&gt;execute();
                    //get user_id of last inserted user because I use user_id as salt
                    //pls feel free to correct me if this is stupid
                    $user_id = $db-&gt;getLastInsertedId();
                    $salt = $user_id; 
                    //hash password with user_id
                    $saltedHash = hash('sha256', $password, $salt);
                    var_dump($saltedHash);

                    //update user with password
                    $db-&gt;prepare("UPDATE user set password = ? where user_id = ?");
                    $db-&gt;bindParams(array($saltedHash, $user_id));
                    $db-&gt;execute();
                    //since I call this function via ajax true -&gt; user is inserted correct
                    echo "true";
                }else{
                    echo "Error: name is already in use";
                }
            }   
            catch(Exception $e) 
            {
                $this-&gt;m_renderer-&gt;loadTemplate('error.html');
                $this-&gt;m_renderer-&gt;assign(array('errorcode' =&gt; 6000, 'errormessage' =&gt; $e-&gt;getMessage()));
                $this-&gt;m_renderer-&gt;render();
            }
        }
    }
</code></pre>

<p>And this is my login function</p>

<pre><code>public function loginUser(){
        if (isset($_POST['name']) &amp;&amp; isset($_POST['password'])){
            try
            {
                $name = $_POST["name"];
                $password = $_POST['password'];

                $db = CDatabase::getInstance();
                $db-&gt;prepare("SELECT * FROM user where LOWER(name) = LOWER(?)");
                $db-&gt;bindParams(array($name));
                $db-&gt;execute();

                $result = $db-&gt;fetch();
                if ($result-&gt;num_rows &gt; 0){
                    $row = $result-&gt;fetch_assoc();
                    $salt = $row["user_id"];        //use user_id as salt
                    $saltedHash = $row["password"];

                    var_dump($password);
                    var_dump(hash('sha256', $password, $salt));
                    var_dump($saltedHash);
                    if (hash('sha256', $password, $salt) ==  $saltedHash) {
                        echo "correct";
                    } else {
                        echo 'Error: password incorrect';

                    }
                }else{
                    echo "Error: username does not exist";
                }
            }
            catch(Exception $e) 
            {
                $this-&gt;m_renderer-&gt;loadTemplate('error.html');
                $this-&gt;m_renderer-&gt;assign(array('errorcode' =&gt; 6000, 'errormessage' =&gt; $e-&gt;getMessage()));
                $this-&gt;m_renderer-&gt;render();
            }
        }
    }
</code></pre>

<p>And this is the output from my console</p>

<pre><code>string(32) "��Ё�L}e�/���Z���O+�,�]l�� //insertUser hash('sha256', $password, $salt);
"
true
string(4) "test"
string(32) "��Ё�L}e�/���Z���O+�,�]l�� //loginUser hash('sha256', $password, $salt)
"
string(31) "????L}e?/???Z???O+?,?]l?? //this value is in my database
"
Error: password incorrect
</code></pre>

<p>I'm using the latest version of XAMPP on my local machine<br><br>Thank you very much</p>