Hmm looks like a phpBB3 table... So you want to pass in a username and return the associated user id?
Anyway, the following should work...
Assumptions:
- the table name is 'user'
- database details are already predefined in the following constants (DB_HOST, DB_USER, DB_PASS, DB_NAME)
PHP Code:
function fetchUserID($username) {
// Establish a database connection
$db = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Clean the input so it's not malicious
$username = $db->real_escape_string($username);
// Construct the SQL query
$sql = "SELECT user_id FROM users WHERE username = '$username'";
// Execute the query in the database
$result = $db->query($sql);
// If there was only one result returned, grab the user_id and return it, otherwise return false
if($result->num_rows == 1) {
$dataArray = $result->fetch_assoc();
return $dataArray['user_id'];
}
return false;
}
$user_id = fetchUserID('Admin');
echo $user_id; // Should print out "2"