Page 1 of 1

GetArray with $inputarr

Posted: Sun Mar 28, 2021 8:09 pm
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?

Re: GetArray with $inputarr

Posted: Sun Mar 28, 2021 8:36 pm
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.

Re: GetArray with $inputarr

Posted: Sun Mar 28, 2021 8:38 pm
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

Re: GetArray with $inputarr

Posted: Sun Mar 28, 2021 10:08 pm
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.

Re: GetArray with $inputarr

Posted: Mon Mar 29, 2021 7:07 pm
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.