Solved

PHP + autogenerate IN() list data in MySQL query

Posted on 2012-03-13
4
448 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now