Solved

PHP + autogenerate IN() list data in MySQL query

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

25 Experts available now in Live!

Get 1:1 Help Now