I have a MySQL table called Users. The rows have 4 fields and look like:
INT(10) | serialized php array | CHAR(24) | CHAR(254)
id | profile | name | email
1 | ..ybl4JFtmhQEQVwQOXjo... | TestName | testmail@test.com
2 | ..hd+F2yZVYPlxmyalrQo... | NameTest | mailtest@test.com
I have a name of a user (TestName) and want to get the unique user id (1). Why does following code not work?
function dataBase() {
$dataBase = @new MySQLi("localhost", "name", "password", "databasename");
if (mysqli_connect_errno()) {
safeExit("Failed to establish connection to MySQL database, error: ".mysqli_connect_error(), 'msgError');
} else {
return $dataBase;
}
}
// connect to database
$db = dataBase();
$sql = 'SELECT
id
FROM
Users
WHERE
name = ?
LIMIT
1';
$stmt = $db->prepare($sql);
if (!$stmt) {
safeExit($db->error, 'msgError');
}
$name = 'TestName';
$stmt->bind_param('s', $name);
if (!$stmt->execute()) {
safeExit($stmt->error, 'msgError');
}
$stmt->bind_result($uid);
$stmt->close();
echo $uid;
This code outputs a zer0. I can exclude syntax errors. Something must be wrong with the query.