GetArray with $inputarr

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

GetArray with $inputarr

Post by rotezecke »

I'm using GetArray method with an input array

Code: Select all

SELECT * FROM quotes_booked qb WHERE qb.qb_webquote_id = ? ORDER BY qb.qb_id ASC
and my array looks like this:

Code: Select all

Array
(
    [0] => 10285
    [1] => 10286
)
I know there is a result for 10285, and I know there is NO result for 10286.

Code: Select all

$bookingsArray = $db->GetArray($bookings_sql,$bookings);
the return array is entirely empty though. What am I doing wrong?
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

Re: GetArray with $inputarr

Post by rotezecke »

I have since modified my sql to build a string of IDs instead of the $inputarr.

Code: Select all

SELECT * FROM quotes_booked qb WHERE qb.qb_webquote_id IN ($bookingsStr) ORDER BY qb.qb_id ASC
This works as expected. Still wonder how to use the $inputarr correctly though.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1921
Joined: Mon Jan 29, 2007 4:47 pm

Re: GetArray with $inputarr

Post by Jo Morg »

Apart from the fact that you didn't offer any clue of what the table structure is, including its name and prefix, if any, along with the columns names, which doesn't allow me to exclude any possible SQL syntax error, there is a misinterpretation of what $db->GetArray() actually does:
  • the GetArray method returns an array of values which represent a result or set of results for the query (the 1st argument the method takes);
  • Assuming that your SQL query is correct, the ? represents a placeholder, binding to a value, which has to be set on the second argument of the method, in the form of an array: the relationship has to be one to one i.e. one value in the array for each placeholder ? in the query string and in the same order as they occur in the said string;
For your code to work you'd have to go with:

Code: Select all

$bookingsArray = $db->GetArray($bookings_sql,array($webquote_id));
Or using the alternate PHP syntax:

Code: Select all

$bookingsArray = $db->GetArray($bookings_sql,[$webquote_id]);
Again assuming that your sql query is correct, you are requesting any existing booking with a qb_webquote_id === $webquote_id which all being as expected should be no more than one.

HTH
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm
Location: Nimbin, Australia

Re: GetArray with $inputarr

Post by rotezecke »

Thanks, so each element in my argument array stands for a ? in the SQL? So there will only ever be one result set. That is different to what I assumed it to be. Thanks for clearing this up.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1921
Joined: Mon Jan 29, 2007 4:47 pm

Re: GetArray with $inputarr

Post by Jo Morg »

Yes to your 1st question, no to the 2nd. The query determines the result set in terms of rows and columns to be returned in the array.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Post Reply

Return to “Developers Discussion”