使用 mysql php pdo 从数据库返回一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11928136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:28:27  来源:igfitidea点击:

return one value from database with mysql php pdo

phpmysqlpdo

提问by Amber Stillwell

Im not trying to use a loop. I just one one value from one column from one row. I got what I want with the following code but there has to be an easier way using PDO.

我不想使用循环。我只是来自一行的一列的一个值。我用以下代码得到了我想要的东西,但必须有一种更简单的方法来使用 PDO。

try {
        $conn = new PDO('mysql:host=localhost;dbname=advlou_test', 'advlou_wh', 'advlou_wh');
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }

$userid = 1;

$username = $conn->query("SELECT name FROM `login_users` WHERE username='$userid'");
$username2 = $username->fetch();
$username3 = $username2['name'];

echo $username3;

This just looks like too many lines to get one value from the database. :\

这看起来像太多行,无法从数据库中获取一个值。:\

采纳答案by asprin

You could create a function for this and call that function each time you need a single value

您可以为此创建一个函数并在每次需要单个值时调用该函数

function getSingleValue($tableName, $prop, $value, $columnName)
{
    $q = $conn->query("SELECT `$columnName` FROM `$tableName` WHERE $prop='".$value."'");
    $f = $q->fetch();
    $result = $f[$columnName];
    return $result;
}

Then you can simply do:

然后你可以简单地做:

$singleValue = getSingleValue('login_users', 'username', $userid, 'name'); // will get you the value

So you need to create that function just once, but can reuse it for different tables with different column names.

因此,您只需创建该函数一次,但可以将其重用于具有不同列名的不同表。

COMMUNITY EDIT:For security reasons, avoid concatenating strings to form an SQL query, use prepared statements instead.

社区编辑:出于安全原因,避免连接字符串以形成 SQL 查询,而是使用准备好的语句。

Thus, instead of the vulnerable

因此,而不是脆弱的

$q = $conn->query("SELECT `$columnName` FROM `$tableName` WHERE $prop='".$value."'");

Use the corresponding code with prepared statements

在准备好的语句中使用相应的代码

$q = $conn->prepare('SELECT :columnName FROM :tableName WHERE :property=:value', [
    'columName' => $columnName,
    'tableName' => $tableName,
    'property' => $prop,
    'value' => $value
]);

回答by Ouadie

You can use fetchColumn():

您可以使用fetchColumn()

$q= $conn->query("SELECT name FROM `login_users` WHERE username='$userid'");
$username = $q->fetchColumn();

回答by Marc B

Just like it's far too much work to have to get into your car, drive to the store, fight your way through the crowds, grab that jug of milk you need, then fight your way back home, just so you can have a milkshake.

就像上车、开车去商店、挤在人群中、抢到你需要的那罐牛奶,然后拼命回家一样,工作量太大了,这样你就可以喝一杯奶昔了。

All of those stages are necessary, and each subsequent step depends on the previous ones having been performed.

所有这些阶段都是必要的,每个后续步骤都取决于之前执行的步骤。

If you do this repeatedly, then by all means wrap a function around it so you can reuse it and reduce it down to a single getMyValue()call - but in the background all that code still must be present.

如果您重复执行此操作,那么无论如何都要围绕它包装一个函数,以便您可以重用它并将其减少为单个getMyValue()调用 - 但在后台,所有代码仍然必须存在。