Pdo stopping at first number in array and returning null

So i have it pdo

$emailLists = implode(",'",$emailListIds);

			$stmt = new Database();

			$query = "SELECT
						es.ID AS SAVE_ID,
						es.SEND_DATE AS CREATED,
						es.NAME AS TITLE,
						es.CAMPAIGN_ID AS CAMPAIGN_ID
					FROM schedule_lists sl
						LEFT JOIN TXT2GIVE_EMAIL_SCHEDULE es ON (sl.email_save_id=es.ID)
					WHERE es.ID IN (:emailList) AND es.CHARITY_ID = :clientID
					GROUP BY es.ID";
			$stmt->query( $query );
			$stmt->bind( ':clientID', $clientId );
			$stmt->bind( ':emailList', $emailLists );
			$stmt->execute();
			$result = $stmt->all();

Open in new window


it keeps returning null and i think i know why.

it seems to be going into line
WHERE es.ID IN (:emailList)

Open in new window

and only going to the first id.

which is this here - View of array
also note this is how the array is created -

foreach($result as $emailSaveID){
			if(!empty($emailSaveID)):
				$emailListIds[] = $emailSaveID['EMAIL_SAVE_ID'];
			endif;
		}

Open in new window


what can i do to solve this ?
Alex LordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Hey Alex,

This is one of those annoyances with PDO and prepared statements - you simply can't do what you want the way you want. The reason is down to the way parameters are passed into a prepared statement. Basically, it'll wrap it in quotes. The format for an IN clause is this:

WHERE IN (1,2,3,4,5)

However, when you pass the parameter in, it get's written like so:

WHERE IN ("1,2,3,4,5")

And that just won't work. There are a couple of workarounds. The first one relies on placeholders (?) rather than named parameters, and you dynamically create your SQL statement to contain as many place holders as there are in your array:

SELECT col1, col2, col3 FROM someTable WHERE id IN (?, ?, ?, ?, ?, ?) AND someCol = ?

The alternative is that you just don't use a parameter for the IN clause:

$sql = sprintf( "SELECT col1, col2, col3 * FROM someTable WHERE id IN (%s) AND someCol = :someVar", implode(',', $emailListIds) );

That will correctly create your SQL statement with a properly formatted IN clause, and use the named parameters for the other parts of the query. When you execute, you no longer need to pass in the $emailListIds because they're now hardcoded into the SQL
1
DarrenSenior Software EngineerCommented:
Hi,

I think the line below is the problem

$emailLists = implode(",'",$emailListIds);

it's putting a' in front of each number but not at the end '105, instead of '105',

Just a thought,

Darren
0
Alex LordAuthor Commented:
Thx for the response i will give these a go
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Chris StanyonWebDevCommented:
Even if you wrap each element in quotes, the problem still exists, because you'd end up with:

IN (" '1', '2', '3', '4' ")
0
Alex LordAuthor Commented:
$emailLists = implode("','",$emailListIds);

			$stmt = new Database();

			$query = printf("SELECT
						es.ID AS SAVE_ID,
						es.SEND_DATE AS CREATED,
						es.NAME AS TITLE,
						es.CAMPAIGN_ID AS CAMPAIGN_ID
					FROM schedule_lists sl
						LEFT JOIN TXT2GIVE_EMAIL_SCHEDULE es ON (sl.email_save_id=es.ID)
					WHERE es.ID IN ('%s') AND es.CHARITY_ID = :clientID
					GROUP BY es.ID",$emailLists );
			$stmt->query( $query );
			$stmt->bind( ':clientID', $clientId );
			//$stmt->bind( ':emailList', $emailListIds );
			$stmt->execute();
			$result = $stmt->all();

Open in new window


this is what i did and im getting error on ajax return,

this is wat it is

"
response text - ↵SELECT↵						es.ID AS SAVE_ID,↵						es.SEND_DATE AS CREATED,↵						es.NAME AS TITLE,↵						es.CAMPAIGN_ID AS CAMPAIGN_ID↵					FROM schedule_lists sl↵						LEFT JOIN TXT2GIVE_EMAIL_SCHEDULE es ON (sl.email_save_id=es.ID)↵					WHERE es.ID IN ('105','106','155','156','159','160','157','158','161','187','188','192','199','218','222','226','227') AND es.CHARITY_ID = :clientID↵					GROUP BY es.ID"

Open in new window

0
Chris StanyonWebDevCommented:
OK. You're using the printf() function instead of the sprintf() function. printf() echoes out the argument, sprintf() returns it - you need to return it so you can store it in your $query variable.

Also, if your IDs are numeric, then there's no need to wrap them in quotes. Just use:

$emailLists = implode(',', $emailListIds);

and then in the sprintf() call:

WHERE es.ID IN (%s) AND ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex LordAuthor Commented:
lol chris i changed to spriint lol my mistake all works correctly
0
Chris StanyonWebDevCommented:
Good news Alex :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PDO

From novice to tech pro — start learning today.