简体   繁体   English

如何将PHP变量插入SQL查询中

[英]How to insert a PHP variable into an SQL query

I have an SQL query 我有一个SQL查询

qry1 = 
"SELECT DISTINCT (forename + ' ' + surname) AS fullname
FROM users
ORDER BY fullname ASC";

This gets forename and surname from a table called users and concatenates them together, putting a space in the middle, and puts in ascending order. 这从名为users的表中获取forename和surname,并将它们连接在一起,在中间放置一个空格,并按升序排列。

I then put this into an array and loop through it to use in a select drop-down list. 然后我将它放入一个数组并循环遍历它以在选择下拉列表中使用。

This works, however, what I now want to do is compare the fullname with a column called username in another table called users. 但是,我现在想要做的是将fullname与另一个名为users的表中名为username的列进行比较。

I'm struggling with how to write the query though. 我正在努力解决如何编写查询的问题。 So far I have... 到目前为止我有......

$qry2 
"SELECT username 
FROM users 
WHERE (forename + ' ' + surname) AS fullname 
=" . $_POST['Visiting'];

Any advice on to what I am doing wrong? 对我做错了什么建议?

Rather CONCAT the two columns together. 而是将两列一起CONCAT Also remember to escape any variables before adding them to your query. 还要记住在将任何变量添加到查询之前将其转义。

$qry2 =
"SELECT username AS fullname
FROM users 
WHERE CONCAT(forename, ' ', surname)
='" . mysqli_real_escape_string($connection, $_POST['Visiting']) . "'";

Where $connection is your current db connection 其中$ connection是您当前的数据库连接

I'm not sure that the use of the declared word 'AS' after 'WHERE' is correct in principle. 我不确定在'WHERE'之后使用声明的单词'AS'原则上是正确的。

if you use MySQL, query should look like this: 如果您使用MySQL,查询应如下所示:

SELECT [columns]
FROM [tables] [AS declareTableName]
WHERE [condition]
GROUP BY [declares|columns]
SORT BY [declares|columns]

But, i think your problem not in the query. 但是,我认为你的问题不在查询中。 Concatenating names in the query is incorrect. 查询中的连接名称不正确。 You must separate string with names in Back-end and than use it in query: 您必须将字符串与后端中的名称分开,然后在查询中使用它:

$names = explode(' ', $_POST['Visiting']);

This might work, assuming you use PDO: 假设您使用PDO,这可能会有效:

$qry2 = "SELECT username FROM users
      WHERE CONCAT(forename, ' ', surname) = '" . $conn->quote($_POST['Visiting']) . "'";

...but you should have a look at the possible vulnerabilities through SQL injections. ...但您应该通过SQL注入查看可能存在的漏洞。

Without knowing which library you use for connecting to the MySQL database, it's impossible to give proper advise about which method you should use for escaping the user's input. 在不知道用于连接MySQL数据库的库的情况下,无法正确建议您应该使用哪种方法来转义用户的输入。 quote is the PDO method for escaping, real-escape-string is the equivalent for MySQLi quote是用于转义的PDO方法, real-escape-string相当于MySQLi

You should really refer to using PDO. 你应该真的参考使用PDO。 When using PDO you can bind parameters to specified parts of your query. 使用PDO时,您可以将参数绑定到查询的指定部分。 PDO also has built-in SQL-injection prevention, which is a great security measure that you won't have to deal with yourself. PDO还具有内置的SQL注入预防功能,这是一种很好的安全措施,您无需自己处理。 I hope this answers your question. 我希望这回答了你的问题。 See my example below. 请参阅下面的示例。

Example: 例:

// Create a new PDO object holding the connection details
$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

// Create a SQL query
$query = "SELECT username FROM users WHERE (forename + ' ' + surname) AS fullname = :visiting;";

// Prepare a PDO Statement with the query
$sth = $pdo->prepare($query);

// Create parameters to pass to the statement
$params = [
    ':visiting' => $_POST['Visiting']
]

// Execute the statement and pass the parameters
$sth->execute($params);

// Return all results
$results = $sth->fetchAll(PDO::FETCH_ASSOC);


If you have any other questions about PDO, please refer to the following: 如果您对PDO有任何其他疑问,请参阅以下内容:

Official PDO documentation: 官方PDO文件:
http://php.net/manual/en/book.pdo.php http://php.net/manual/en/book.pdo.php

Documentation on how to bind variables: 有关如何绑定变量的文档:
http://php.net/manual/en/pdostatement.bindparam.php http://php.net/manual/en/pdostatement.bindparam.php

You can use this construction (without "AS fullname" and with apostrophes around variable): 您可以使用此构造(没有“AS fullname”和变量周围的撇号):

$qry2 "SELECT username FROM users WHERE (forename + ' ' + surname) = '" . $_POST['Visiting'] . "'";

But for better security (SQL injection) You should use the escaping of variable. 但为了更好的安全性(SQL注入),您应该使用转义的转义。 For example this construction, if You use MySQL database: 例如,这种构造,如果您使用MySQL数据库:

$qry2 "SELECT username FROM users WHERE (forename + ' ' + surname) = '" . mysql_real_escape_string($_POST['Visiting']) . "'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM