Solved

Filter a PHP/MySQL recordset as I did in Classic ASP with ADO Filter

Posted on 2010-11-27
4
736 Views
Last Modified: 2012-05-10
Hello,
In the good old Classic ASP days I used sometimes to filter a recordset like this:
myRS = "SELECT myid, myname, mykey FROM mytable" ' just to give the idea
...
myRS.Filter = "mykey = " & myfirstvariable
...[loop through filtered records]...
myRS.Filter = "mykey = " & mysecondvariable
...[loop through filtered records]...

Open in new window

In this way, I had to create a recordset only once, instead of creating multiple recordsets:
myRS1 = "SELECT myid, myname, mykey FROM mytable WHERE mykey = " & myfirstvariable
...[loop]...
myRS2 = "SELECT myid, myname, mykey FROM mytable WHERE mykey = " & mysecondvariable
...[loop]...

Open in new window


Well, is there a similar "filter" capabilities in PHP/MySQL, or should I create so many recordset as are my filters?
Thanks
0
Comment
Question by:ivanhalen
  • 2
4 Comments
 
LVL 13

Expert Comment

by:darren-w-
ID: 34221590
Hi,

I think this could be done with place markers as in example 2 in this link?  
http://php.net/manual/en/pdo.prepare.php

Darren
0
 
LVL 12

Expert Comment

by:zappafan2k2
ID: 34221777
How about using "IN" instead of "="?
// assuming you have a list of keys in array $myKeys
$myRS = "SELECT myid, myname, mykey FROM mytable WHERE mykey IN (" . implode(',', $myKeys) . ");";
// loop

Open in new window

0
 

Author Comment

by:ivanhalen
ID: 34221910
@darren-w-: this seems a nice solution, but what about without PDO?
@zappafan2k2: mmm... I can't understand how your example could be useful to me... the idea is to reach something like:
create a general recordset
filter only records that match a certain parameter X
loop only the filtered records X
re-filter only records that match a certain parameter Y
loop only the filtered records Y
etc...

Open in new window

I need this becouse my HTML structure MUST be so:
<table>
// here goes the first filter
<tbody>
// loop through the filtered records
</tbody>
// here goes the second filter
<tbody>
// loop through the filtered records
</tbody>
etc...
</table>

Open in new window

And no, I can't put my records in a single <tbody>...</tbody>, I MUST use as many as my filters are
0
 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 34221955
you could use http://www.php.net/manual/en/mysqli.prepare.php instead of pdo
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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

943 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

18 Experts available now in Live!

Get 1:1 Help Now