Хотя бы как-то так, но не mysql_connect(). Через пару месяцев ее вообще из языка выпилят.
<?php
$config = [
'DB_USER' => 'root',
'DB_PASSWORD' => '****',
'DB_HOST' => 'localhost',
'DB_DRIVER' => 'mysql',
'DB_DATABASE' => 'test'
];
$pdoConfig = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
];
$dsn = $config['DB_DRIVER'] . ':host=' . $config['DB_HOST']
. ';dbname=' . $config['DB_DATABASE'];
try {
$pdo = new PDO(
$dsn,
$config['DB_USER'],
$config['DB_PASSWORD'],
$pdoConfig
);
$query = 'SELECT `username` FROM `data2` WHERE `username` = :username';
$stmt = $pdo->prepare($query);
$stmt->bindParam(':username', $_GET['username'], PDO::PARAM_STR);
$stmt->execute();
$user = $stmt->fetch();
echo json_encode($user);
} catch (PDOException $e) {
echo $e->getMessage();
}