PHP函数从数据库中提取字段值

PHP函数从数据库中提取字段值

本文介绍了PHP函数从数据库中提取字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用php函数获取mysql数据库的值.我有一个这样的数据库:

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  | [email protected]  | password  | other
-------------------------------------------------------
example2 | [email protected] | password2 | other2

在我的PHP文件中,我尝试使用此功能:

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("[email protected]", "name", $connection);

但是结果是我只得到字段名,而不是它的内容(在本例中,我得到的是"NAME"而不是"example").如何获取数据库单元格的内容?

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?

推荐答案

$ field周围没有引号.

No quotes around $field.

"SELECT $field FROM users WHERE email='$email' LIMIT 1";

这篇关于PHP函数从数据库中提取字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:50