OCI Bound Variables

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Netherfox
Forum Newbie
Posts: 6
Joined: Tue Jan 15, 2008 12:32 pm

OCI Bound Variables

Post by Netherfox »

Hello all,

Would anyone happen to know how to use OCI bound variables in a SQL "where in" context?
For example:

Code: Select all

select * from employee where id in (1, 2, 3, 4, 5);
Basically I am working with a string $_GET variable which would tell me those IDs, so it might be "1, 3, 4" or "1, 2" or whatever, but I need to be able to select based on those IDs, using bound variables for performance.

In effect, I would try something like this:

Code: Select all

$theDB = OCILogon ("employee", "employee", "employee");
$sql = "select * from employee where id in (:list)";
 
$resource = OCIParse($theDB, $sql);
OCIBindByName($resource, ":list", $_GET['list']);
OCIExecute($resource);
Of course, this doesn't work because that list is supposed to be integers. How can I get around this?
Netherfox
Forum Newbie
Posts: 6
Joined: Tue Jan 15, 2008 12:32 pm

Re: OCI Bound Variables

Post by Netherfox »

So I discovered there basically is no way to do that per se...

But, this is how I basically accomplished the same objective though. Hopefully this will help somebody out in the future:
(assuming $_GET['list'] = "1, 2, 3")

Code: Select all

$theDB = OCILogon ("employee", "employee", "employee");
$sql = "select * from employee where id in (:list)";
$resource = OCIParse($theDB, $sql);
$myList = explode (",", $_GET['list']);
 
foreach($myList as $myListItem)
{
   OCIBindByName($resource, ":list", $myListItem);
   OCIExecute($resource);
   $myRecord = oci_fetch_array($resource);
   // Do something with $myRecord
}
OCIFreeStatement($resource); // just added this to clarify this must be freed after the loop if being used.
Post Reply