Home Backend Development PHP Tutorial 小白继续。PHP中怎么把数组作为sql查询语句的条件

小白继续。PHP中怎么把数组作为sql查询语句的条件

Jun 13, 2016 pm 01:38 PM
and array implode name quot

小白继续求助。。PHP中如何把数组作为sql查询语句的条件。
小白感谢各位大大对于自己上一个帖子的帮助,终于弄明白是怎么回事了。 现在遇到了另一个问题:

我通过一个页面让用户提交了12个多选列表,
获取出来就是12个数组,

然后根据这12个数组里的内容进行数据库搜索,

可是发现好像没有办法把这12个数组里的内容一项一项提取出来,然后写sql查询语句。。

比如:

$a = array(a,b,c,d);
$b = array(1,2,3,4);
$c = array(I,II,III,IV);
然后我想select * from 表名 where name=a AND name=b AND name=c AND name=d AND name=1 AND name=2..... AND name=IV

不知道如何可以实现呢?用foreach循环的话,只怕每次都是当前元素的值,这样查出来是每一个当前元素可以搜索到的结果。。。

但是我想查询的是所有数组里所有元素存在的条件下,最后的结果。。

求助各位高人!


------解决方案--------------------
呵呵,那么多AND可能找到东西么?

PHP code
$a = array('a','b','c','d');
$b = array(1,2,3,4);
$c = array('I','II','III','IV');
#保证$a,$b,$c任意一个非空数组,自己判断                                                                                                      
echo "select * from table where name='".implode("' OR name='",array_merge($a,$b,$c))."'";
<br><font color="#e78608">------解决方案--------------------</font><br>
在吃饭,给你提示<br>1个是将数组暑输出字符串 implode(',',$arr) 标点符号什么的自己再加工吧<br>1个是修改你的SQL 语句 将同个数组AND 什么的修改成 IN  <br>例如 IN ('a','b','c','d')<br><br>即可
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

引用:

在吃饭,给你提示
1个是将数组暑输出字符串 implode(',',$arr) 标点符号什么的自己再加工吧
1个是修改你的SQL 语句 将同个数组AND 什么的修改成 IN
例如 IN ('a','b','c','d')

即可


谢谢回复! 如果是name IN ('a','b','c','d'),那么应该是OR的关系吧?还是……
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert array to string using implode() function in PHP Convert array to string using implode() function in PHP Jun 26, 2023 pm 11:51 PM

In PHP, we often need to combine elements in an array into a string. At this time, the implode() function comes in handy. The implode() function converts the elements in the array into strings and concatenates them to produce a complete string. This article will introduce how to use the implode() function in PHP to convert an array into a string. 1. Basic usage of implode() function The implode() function has two parameters. The first parameter

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

PHP Warning: implode(): Invalid arguments passed solution PHP Warning: implode(): Invalid arguments passed solution Jun 23, 2023 am 09:04 AM

In PHP programming, Warning messages often appear. One of the more common prompts is "PHPWarning:implode():Invalidargumentspassed". This article will introduce the ideas and methods to solve this Warning. First, we need to understand how to use the implode() function. The function of this function is to concatenate an array into a string, using the following syntax: string

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

Introduction to the usage of PHP implode() function Introduction to the usage of PHP implode() function Jun 27, 2023 am 10:56 AM

In PHP programming, the implode() function is a very commonly used function. This function is mainly used to concatenate elements in an array to form a string. The use of this function is very simple and flexible. It allows us to save time and code in the process of splicing strings. In this article, we will introduce PHP's implode() function in detail so that we can use it better. Basic syntax The basic syntax for using the implode() function in PHP is as follows: implode(separa

See all articles