/    Sign up×
Community /Pin to ProfileBookmark

PDO database query array error

Hello
PDO database query array error
PHP Warning: count(): Parameter must be an array or an object that implements Countable in
PHP Warning: str_repeat(): Second argument has to be greater than or equal to 0 in
PHP Warning: array_merge(): Argument #2 is not an array in
PHP Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in
PDOStatement->execute(NULL)

$_SESSION[‘satellite_ids_json’] = [“1″,”2″,”3″,”4”]

“`
$arr = json_decode($_SESSION[‘satellite_ids_json’]);
$in = str_repeat(‘?,’, count($arr) – 1) . ‘?’;
$sql = “SELECT * FROM satellite WHERE satellite_lnb_type=? AND id IN ($in)”;
$stm = $PDOdatabase->prepare($sql);
$params = array_merge([‘MDULNB’], $arr);
$stm->execute($params);
“`

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 16.2020 — > @gecekule#1624299 PHP Warning: count(): Parameter must be an array or an object that implements Countable in

Start by figuring out why $arr is not an array or iterable object at that point. Maybe do a var_dump() of it after the json_decode() to see what you actually have?
Copy linkTweet thisAlerts:
@gecekuleauthorOct 17.2020 — var_dump($arr);

array (size=4)

0 => string '1' (length=1)

1 => string '2' (length=1)

2 => string '3' (length=1)

3 => string '4' (length=1)

It always gave an error yesterday, today it didn't fail I couldn't understand the problem

Thank you
Copy linkTweet thisAlerts:
@gecekuleauthorOct 17.2020 — I solved the problem,

I added this to the include file, the problem is fixed.

``<i>
</i>if(!isset($_SESSION)){
session_start();
}<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogOct 17.2020 — Glad you figured it out. While that session check is probably fine, you might want to consider this as being a bit more robust (and maybe a bit more explicit?):
<i>
</i>if(session_status() == PHP_SESSION_NONE) {
session_start();
}
Copy linkTweet thisAlerts:
@gecekuleauthorOct 19.2020 — Once the problem was fixed, then the problem happened again I couldn't understand

So, while there is data in the session, it acts as if it doesn't exist.

So I looked for different ways

Satellite ids come as arrays on another page and creating json on the same page

I'm using the above code on another page

There is "$ _SESSION ['satellite_ids_json']" data here but I don't understand it gives an error

I used it in the above code by taking it into the session as an array on the satellite page.

I'll try with this check
Copy linkTweet thisAlerts:
@NogDogOct 19.2020 — Just because this came up recently elsewhere, be careful with use of sub-domains (include www.), as that can impact whether or not the client sends the session cookie to a given page. It _might_ explain why it would work in some situation and not in others, as if some links just specify "yourdomain.com' but others hit 'www.yourdomain.com', you may not receive the session cookie back from the browser. You can help prevent any such problem with:
<i>
</i>&lt;?php
session_set_cookie_params(60 * 60 * 24, '/', '.yourdomain.com'); // note leading dot for domain
session_start();

This is a useful thing to stick into any start-up script you require on each page, as the cookie will then be exchanged regardless of whether any sub-domain is specified or not.
×

Success!

Help @gecekule spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...