从 SELECT PHP 设置变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4930961/
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 16:17:42  来源:igfitidea点击:

Set variable from SELECT PHP

phpmysql

提问by Mike

I simply want to define the variable "$read" as whatever its value is in the database. How can I do this?

我只是想将变量“$read”定义为它在数据库中的任何值。我怎样才能做到这一点?

$read =  "SELECT `read` FROM `users` WHERE `id` = '$id'";

回答by Dan Grossman

$read = mysql_result(mysql_query("SELECT read FROM users WHERE id = $id"),0);

回答by Sebastian Paaske T?rholm

Beware of the answers given using mysql_query, as they're vulnerable to SQL injection.

请注意使用 给出的答案mysql_query,因为它们容易受到SQL 注入的影响

If $idis supplied by a user, you should neverdirectly put it into the SQL query, but rather use a prepared statement.

如果$id由用户提供,则永远不要将其直接放入 SQL 查询中,而应使用准备好的语句。

One way of doing this, is by using PDO, in a manner similar to this:

这样做的一种方法是使用PDO,方式类似于:

$dbh = new PDO($connStr, $user, $pass);
$sql = "SELECT `read` FROM `users` WHERE `id` = :id";
$statement = $dbh->prepare($sql);
$statement->execute( array('id' => $id) );
$read = $statement->fetchColumn();

For more information on how to use PDO, see the following:

有关如何使用 PDO 的更多信息,请参阅以下内容:

回答by Miron Vranje?

One way to accomplish this is as follows:

实现此目的的一种方法如下:

// Run the query
$db_result = mysql_query("SELECT read FROM users WHERE id = $id");
// Get the first row (in this case you'll only get one row)
$row = mysql_fetch_array($db_result, MYSQL_NUM);
// Get the first column (you should only have one column anyway) and put it into your variable
$read = $row[0];

As pointed out below, I should add that if you don't trust $id to be properly escaped, you could be vulnerable to SQL injection. To overcome this, you should either make sure you properly escape and validate $id or use some kind of binding or prepared statement to do it for you, like in this questionor in the example below.

正如下面所指出的,我应该补充一点,如果您不相信 $id 会被正确转义,您可能容易受到SQL injection 的攻击。为了克服这个问题,您应该确保正确转义并验证 $id 或使用某种绑定或准备好的语句来为您执行此操作,例如在此问题或下面的示例中。

回答by Your Common Sense

I know it's almost impossible to teach someone something, especially if they don't want to learn. But in hope it will be useful for someone else

我知道教别人东西几乎是不可能的,特别是如果他们不想学的话。但希望它对其他人有用

All modern programming languages supports such a thing called "user defined functions".
A very handy feature.

所有现代编程语言都支持这种称为“用户定义函数”的东西。
一个非常方便的功能。

A programmer, who wants to have their code real neat, can make a function out of some repetitive code and make calling this code REAL small, just almost as it was phrased in the OP:

一个程序员,想要让他们的代码真正整洁,可以用一些重复的代码制作一个函数,并使调用这个代码真正小,就像它在 OP 中所说的那样:

$read = dbgetvar("SELECT `read` FROM `users` WHERE `id` = %d",$id);

another benefit from such an approach - your code could contain all necessary things, like parameter sanitization and error handling. And still calling this code would be shorter than all codes above, made ugly and unmantainable in pursue for shortness.

这种方法的另一个好处 - 您的代码可以包含所有必要的内容,例如参数清理和错误处理。并且仍然调用此代码将比上面的所有代码都短,为了追求简短而变得丑陋且难以维护。

An example of such a function

这种函数的一个例子

function dbgetvar(){
  $args = func_get_args();
  $query = array_shift($args); 
  $query = str_replace("%s","'%s'",$query); 
  foreach ($args as $key => $val) { 
    $args[$key] = mysql_real_escape_string($val); 
  } 
  $query = vsprintf($query, $args);

  $res = mysql_query($query);
  if (!$res) {
    trigger_error("dbget: ".mysql_error()." in ".$query);
    return FALSE;
  }
  $row = mysql_fetch_row($res)
  if (!$row) return NULL;
  return $row[0];
}

回答by Anri?tte Myburgh

I would do the following:

我会做以下事情:

// leave the single quotes around $id because it most probably is an INT
// LIMIT 1 will make the query a bit faster
$result = mysql_query("SELECT `read` FROM `users` WHERE `id` = $id LIMIT 1");

$row = mysql_fetch_row($result);
$read = $row[0];

Hope it works for you.

希望对你有效。

回答by Or Weinberger

Assuming there is only 1 result:

假设只有 1 个结果:

$read = mysql_fetch_array(mysql_query("SELECT read FROM users WHERE id = $id"));
$read = $read[0];