Solved

PHP + autogenerate IN() list data in MySQL query

Posted on 2012-03-13
4
458 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 125 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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