Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP + autogenerate IN() list data in MySQL query

Posted on 2012-03-13
4
Medium Priority
?
461 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

609 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