PHP/MySQLi - Function to get one variable doesn't work - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: Other (
https://sampforum.blast.hk/forumdisplay.php?fid=7)
+--- Forum: Everything and Nothing (
https://sampforum.blast.hk/forumdisplay.php?fid=23)
+--- Thread: PHP/MySQLi - Function to get one variable doesn't work (
/showthread.php?tid=495986)
PHP/MySQLi - Function to get one variable doesn't work -
Mado - 19.02.2014
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?
Re: PHP/MySQLi - Function to get one variable doesn't work -
Vince - 19.02.2014
You forgot to store the result.
Re: PHP/MySQLi - Function to get one variable doesn't work -
GWMPT - 19.02.2014
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.
Re: PHP/MySQLi - Function to get one variable doesn't work -
Mado - 19.02.2014
Quote:
Originally Posted by Vince
You forgot to store the result.
|
What do you mean by that?