Newbie problem with prepared statements

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a table with five columns, named col0 to col4. I want to store
each element in myarray[] in those columns. The problem is that each
time the script is runs myarray[] can have between one and five
elements in it.

$query = "
   SELECT id
   FROM table_name
   WHERE col0 = ? AND col1 = ? AND col2 = ? AND col3 = ? AND col4 = ?
if ($stmt->prepare($query)) {
   $stmt->bind_param('iiiii', $myarray[0], $myarray[1], $myarray[2],
$myarray[3], $myarray[4]);

What I'm looking to do is if the script happens to have only 3
elements in $myarray on a given run through, the query will search for
a record with values in col0-col2 and NULL values in col3 & col4. Is
there a way to do this coming up with specific queries for each
situation, without resorting too if(count($myarray) == 1) {... },
if(count($myarray) == 2) {... }, if(count($myarray) == 3) {... } and
so on?

Or am I trying to get around the whole point of using prepared
statements in the first place by dynamically choosing how many columns
I'm working with?

Many thanks!

Re: Newbie problem with prepared statements

clumsy_ninja wrote:
Quoted text here. Click to load it

Sounds like you have a bigger problem in that your database is not
properly normalized.

Do some searches on "database normalization", and ask for suggestions on
your design in the appropriate database newsgroup (i.e.
comp.databases.mysql for MySQL database).  A redesign will resolve your

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Newbie problem with prepared statements

Thanks for the lead, Jerry. I'll look into it right now.

Site Timeline