实现从数据库中的数组 [英] making an array from database

查看:124
本文介绍了实现从数据库中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要疯了这个问题,我想用逗号(串),如例如分离的数组:从数据库(1,2,4,6),并插入此像一个查询中的变量(从表中选择其中id IN($变量字符串)。谁能帮助我?

I'm going crazy with this issue, I want to get an array separated by commas(string) like example:(1,2,4,6) from a database and insert this as a variable inside a query like(select from table where id IN($variable string). Can anyone help me with this?

例如:

<?php
//connect
$sql = "select id from users where id=1";
//get the results as an array

插入此查询里面的那个变量

insert that variable inside this query

select from table where id IN($variable);
//
?>

我试过多种方式,总是有不同的问题。请需要一个答案!

i tried multiple ways and always have different issues. Please need an answer!

推荐答案

破灭()变换数组到由您选择的字符分隔值的字符串:

implode()transforms an array into a string of values separated by the char you choose:

$arr = array(1,2,3,4,5,6,7,8,9); // make an array
$list = implode(',',$arr);
$sql = "SELECT * FROM table WHERE id IN($list)";

这篇关于实现从数据库中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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