I'm trying to get a value of a mysql database using a php function. I have a database like this:
NAME | EMAIL | PASSWORD | OTHER
-------------------------------------------------------
example | example@example.com | password | other
-------------------------------------------------------
example2 | example2@example.com | password2 | other2
and in my PHP file I've tried to use this function:
function selectUserField($email, $field, $connection){
$select_user = "SELECT '$field' FROM users WHERE email='$email' LIMIT 1";
$result = mysqli_query($connection, $select_user);
$value = mysqli_fetch_assoc($result);
return $value[$field];
}
//And I try to echo the result of the function
$connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
echo selectUserField("example@example.com", "name", $connection);
But as result I get only the field name and not its content (for this example I get " NAME
" and not " example
"). How can i do to get the content of the database cell?
Try this
function selectUserField($email, $field, $connection){
$select_user = "SELECT `$field` FROM users WHERE `email`='$email' LIMIT 1"; //wrap it with ` around the field or don't wrap with anything at all
$result = mysqli_query($connection, $select_user);
$value = mysqli_fetch_assoc($result);
return $value[$field];
}
//And I try to echo the result of the function
$connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
echo selectUserField("example@example.com", "name", $connection);
$ field周围没有引号。
"SELECT $field FROM users WHERE email='$email' LIMIT 1";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.