PHP/MySQLi - Function to get one variable doesn't work
#1

Hi,

I tried to create this function which is included to another page. It must return the ID of a user depending on the name of the user. This is the function:

PHP код:
function GetUserID($AccName){
    global 
$server;
    global 
$user;
    global 
$pw;
    global 
$database;
    
$sql_query mysqli_connect($server$user$pw$database) or die("Error " mysqli_error($sql_query));
    
    
$AccName mysqli_real_escape_string($sql_query$AccName);
    
    
$search "SELECT UniqueID FROM Accounts WHERE UserName = '".$AccName."';";
    
    if (
$result mysqli_query($sql_query$search)) {
        while (
$row mysqli_fetch_row($result)) {
            
$id $row[0];
        }
        
mysqli_free_result($result);
    }
    
mysqli_close($sql_query);
    return 
$id;

This is how it is called:
PHP код:
$_SESSION['id'] = GetUserID($Name); 
The problem is that it doesn't return anything. I'm working on this issue for around an hour now and didn't find anything to solve it so far.. Anyone with ideas?
Reply
#2

You forgot to store the result.
Reply
#3

you should use something like:
PHP код:
function GetUserId($name) {
    global 
$server$user$pw$database;
    
$sql = @mysqli_connect($server$user$pw$database);
    
$name mysqli_real_escape_string($sql$name);
    
$data mysqli_query($sql"SELECT `UniqueID` FROM `Accounts` WHERE `UserName` = '{$name}'");
    
mysqli_store_result($sql);
    
$fetch mysqli_fetch_array($data);
    @
mysqli_close($sql);
    return 
$fetch['UniqueID'];

That should work, not sure, because i wrote everything in the "text area", and not in a IDE.
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
You forgot to store the result.
What do you mean by that?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)