PHP / MySQL的使用WHERE子句中的数组 [英] PHP/MySQL using an array in WHERE clause

查看:237
本文介绍了PHP / MySQL的使用WHERE子句中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于ID数组 $画廊=阵列(1,2,5)我想有一个使用数组的值的WHERE子句中的SQL查询这样的:

SELECT *
从画廊
WHERE数组$画廊ID = / *值...例如。 (1 || 2 || 5)* /

我怎么能生成这种查询字符串与MySQL使用?


解决方案

  

当心!这个答案包含了一个严重的 SQL注入漏洞。不要使用这里psented的code样品$ P $。这个答案只有preserved至prevent打破链接和参考价值。


  $ IDS =加入('',$画廊);
$ SQL =SELECT * FROM WHERE画廊身份证件($ IDS);

Given an array of ids $galleries = array(1,2,5) I want to have a SQL query that uses the values of the array in its WHERE clause like:

SELECT *
FROM galleries
WHERE id = /* values of array $galleries... eg. (1 || 2 || 5) */

How can I generate this query string to use with MySQL?

解决方案

BEWARE! This answer contains a severe SQL injection vulnerability. Do NOT use the code samples presented here. This answer is only preserved to prevent breaking links and for reference value.

$ids = join(',',$galleries);  
$sql = "SELECT * FROM galleries WHERE id IN ($ids)";

这篇关于PHP / MySQL的使用WHERE子句中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆