I was trying to write a new function after having not worked with SQL in a long time, so I used an existing function as a template to write the new one. It’s a simple enough concept, look for all entries based on a few constraints and return it as an array. First here is the function that works:
[php]
function find_sizes_for_image($index)
{
global $db;
$sth = $db->prepare(‘SELECT sizeID FROM inventory WHERE imageID = :index’);
$sth->bindParam(’:index’,$index,PDO::PARAM_INT);
$sth->execute();
$listcounter=0;
foreach($sth as $entry)
{
$list[$listcounter][‘size’] = $entry[‘sizeID’];
$list[$listcounter][‘desc’] = get_size($entry[‘sizeID’]);
$listcounter++;
}
$sth->closeCursor();
return $list;
}
[/php]
Based on that, I came up with this for the new function:
[php]
function get_calendar($month, $year)
{
global $db;
$sth = $db->prepare(‘SELECT * FROM calendar WHERE date-year = :date-year AND date-month = :date-month’);
$sth->bindParam(’:date-year’,$year,PDO::PARAM_INT);
$sth->bindParam(’:date-month’,$month,PDO::PARAM_INT);
$sth->execute();
$list_counter=0;
foreach($sth as $entry)
{
$list[$list_counter][‘date_year’] = $entry[‘date-year’];
$list[$list_counter][‘date_month’] = $entry[‘date-month’];
$list[$list_counter][‘date_day’] = $entry[‘date-day’];
$list[$list_counter][‘time_start’] = $entry[‘time-start’];
$list[$list_counter][‘time_end’] = $entry[‘time-end’];
if($entry[‘available’] && !$entry[‘reserved’])
{
$list[$list_counter][‘available’] = true;
}
else if(!$entry[‘available’] && $entry[‘reserved’])
{
$list[$list_counter][‘available’] = false;
}
$list[$list_counter][‘info’] = $entry[‘info’];
$list_counter++;
}
$sth->closeCursor();
return $list;
}
[/php]
I’m trying to figure out what the critical difference is. The first one only has one bindParam while the second has two. The first one only gets one value, while the second one gets all the values in the rows selected. Other than that, I don’t know. It’s always hard to come back and write code when you haven’t done it in a year.
Incidentally, the thing fails on the line $sth->execute(); if it makes a difference.