How to Get an Unknown Username Given an Id

How can I get an unknown username given an ID?

I would strongly recommend that you avoid the mysqli extension. You should use some database abstraction library instead of using the mysqli functions directly; the mysqli class is not suited to be used on its own.

If you really want to do it purely with the mysqli class, you have few options.

First, you need to open the connection properly. These 3 lines ensure you have the connection ready:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // enable error reporting
$mysqli = new mysqli('localhost', 'username', 'password', 'dbname');
$mysqli->set_charset('utf8mb4'); // always set the charset

Then you need to prepare a statement, bind the parameter and execute it.

$id = '10527391670258314752';
$stmt = $mysqli->prepare('SELECT username FROM user_data WHERE id=?');
$stmt->bind_param('s', $id);
$stmt->execute();

Once the statement is executed you need to fetch the results. If you have only one variable you could simply use bind_result()

$stmt->bind_result($_SESSION['name']);
$stmt->fetch();
echo $_SESSION['name'];

However, this approach is not recommended and not very flexible. Instead it's better to fetch the whole result set and get an array containing the first row.

$result = $stmt->get_result();
$row = $result->fetch_array();
$_SESSION['name'] = $row['username'];
echo $_SESSION['name'];

As an alternative with PDO the same code would look like this:

session_start();

$pdo = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass, [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_EMULATE_PREPARES => false,
]);

$id = '10527391670258314752';

$stmt = $pdo->prepare('SELECT username FROM user_data WHERE id=?');
$stmt->execute([$id]);
$_SESSION['name'] = $stmt->fetchColumn();
echo $_SESSION['name'];

How to retrieve the user name from the user ID

You use getpwuid to look up the password file entry for a particular UID (which includes the user name, but now not the password itself) and getgrgid to look up the group file entry for a particular GID.

php :how i can get the unknown id of an username and password in mysql

Edit: Sorry one of my first times answering a question on here. I will include an explanation of what is happening in all future answers.

basically when you use the mysqli_fetch_array() function you are returning two arrays for the first row of the query. One associative array, key=field name + value = return value. One numbered array starting from index 0. You specified to only have the associative array by using the 2nd optional parameter.

So to access the first field of the query in this can you can use $row['ID'] or $row[0]. I prefer to use the associative array as it makes the code easier to read.

You can't use this to access the fields names or password as they are not in the SELECT clause of your query. If you wanted to access them you could use the following

$sql = "SELECT ID, names, password FROM newdata WHERE  names = '$myusername' and password= '$mypassword' ";

If you return multiple rows of data you can access each row by wrapping your fetch_array inside of a while loop

while($row = mysqli_fetch_array($result)){
//do some stuff here
}

How to get only a text corresponding to the id from MySQL and store it as a variable in PHP

Assuming you use MySQL, the table is named users and you are using PDO, this would get what you need:

$stmt = $conn->query("SELECT * FROM users WHERE ID = 2");
$row = $stmt->fetch()
$account = $row['ACCOUNT']

unknown attribute username where username does exist

It seems you need to compare case-insensitively, whilst ruby compares case-sensitively by default. This should solve your problem here:

Given /^a user "(.*?)" exists$/ do |user_name|
@user = User.create!(:username => user_name.downcase! , :password => "s3cr3t")
end

Are you running a migration (below) before running the test?

env RAILS_ENV=test rake db:migrate

Display user name in reference to user id in django template

I think you're looking for a simple template tag.

from django import template
from django.contrib.auth.models import User

register = template.Library()

@register.simple_tag
def get_username_from_userid(user_id):
try:
return User.objects.get(id=user_id).username
except User.DoesNotExist:
return 'Unknown'

Which you would use like:

{% for version in version_list %}
{% get_username_from_userid version.user_id %}
{% endfor %}

How to get the currently logged in user's user id in Django?

First make sure you have SessionMiddleware and AuthenticationMiddleware middlewares added to your MIDDLEWARE_CLASSES setting.

The current user is in request object, you can get it by:

def sample_view(request):
current_user = request.user
print current_user.id

request.user will give you a User object representing the currently logged-in user. If a user isn't currently logged in, request.user will be set to an instance of AnonymousUser. You can tell them apart with the field is_authenticated, like so:

if request.user.is_authenticated:
# Do something for authenticated users.
else:
# Do something for anonymous users.

Find other column's value with their ids

Try this one

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli_connect = mysqli_connect("localhost", "root", "", "hexll");

$sqlquery= mysqli_query($mysqli_connect,"select text from mytable where id='1' ");

$row= mysqli_fetch_array($sqlquery, MYSQLI_ASSOC);

echo $row['text'];


Related Topics



Leave a reply



Submit