?
Solved

PHP + autogenerate IN() list data in MySQL query

Posted on 2012-03-13
4
Medium Priority
?
460 Views
Last Modified: 2012-04-01
I have developed a query that I use to generate a report for multiple users.  Ideally, it would be better to use PHP to upload these list of users, then have that list automatically  added to an IN statement in a query.

For example:
Select fname, lname
From usertable
Where myuser.id IN ('123', '234', '567')  *This is the data I'd like to pull in from a CSV file
Order by lname

I've written the code to create the upload file and place the CSV contents into a temporary table.  How do I get that table data into the IN() statement.
0
Comment
Question by:MartiJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Derokorian
ID: 37716541
Can you show an example of the CSV file? Or at least the data your are working with that you get from the file?
0
 

Author Comment

by:MartiJ
ID: 37717261
I'm given a CSV file

id           firstname              lastname
1925      Ima                        User
1878      Hesad                    Eveloper

I pull the id's into a temporary database, but I need to then add those id's to the IN().  Maybe I can use a variable that will hold an array?  I'm really weak with arrays.

~m
0
 
LVL 10

Accepted Solution

by:
Derokorian earned 375 total points
ID: 37719859
Well if you put all the IDs into an array, called say $ids you could make the IN() like this:
$sql = 'Select fname, lname
From usertable
Where myuser.id IN ('. implode(',',$ids) .')
Order by lname';

Open in new window

See http://www.php.net/implode for more information about how this function works. If you show me how you fetch the data from the CSV (sorry should have said that first) I can alter it to make the $ids array for you.
0
 

Author Closing Comment

by:MartiJ
ID: 37793318
Thank you.  The code you provided helped me finish the script.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question