V
V
Vladislav Tikhonov2017-01-12 18:01:57
PHP
Vladislav Tikhonov, 2017-01-12 18:01:57

PHP. How to get value from database column?

Hello. I got one problem here.
With this line I get id by username:

$passwordByID = mysql_query("SELECT password FROM users WHERE id = '".$IdByName."'");

But it doesn't give me what I want. Gives me "Resource id #8", and I need exactly the value.
So that I can verify it with the entered password.
Something like this:
if($password == $passwordByID) {
    //все правильно
}

-----------------------------
Full code:
<?php

require 'db_connect.php';

if($_POST['username'] == "")
{
  echo("Введите логин.");
} else {
  $username = $_POST['username'];
}

if($_POST['password'] == "")
{
  echo("Введите пароль.");
} else {
  $password = md5($_POST['password']);
}


if(isset($username) && isset($password))
{
  $q1 = mysql_query("SELECT * FROM users WHERE username = '".$username."'");
  if(mysql_num_rows($q1) == 1) {
    $IdByName = mysql_query("SELECT id FROM users WHERE username = '".$username."'");
    
    $passwordByID = mysql_query("SELECT password FROM users WHERE id = '".$IdByName."'");
    if($password == $passwordByID)
    {
      echo "Вы успешно авторизовались.";
    } else {
      echo "Вы ввели неверный пароль.";		}
  } else {
    echo "Пользователя с таким именем не существует.";
  }
}

?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2017-01-12
@Sanasol

php.net/manual/en/mysqli.query.php
php.net/manual/en/function.password-hash.php

M
Maxim Timofeev, 2017-01-12
@webinar

1st mistake: do not store the password in the database as a password, store the hash of the password, user id and secret key for example.
2nd: Mysql gives out not what you want, but what you request, in my opinion there are too many quotes and you are looking for id = line .$IdByName. instead of the value from the variable, as needed.
3rd: Why are you sure that $passwordByID is not the correct result, and not $IdByName or $password? Dump all three variables through var_dump and see what's in them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question