/    Sign up×
Community /Pin to ProfileBookmark

bind param one by one

Is is possible to make this

[code]$stmt->bind_param(“sss”, $one,$two,$three);[/code]

This

[code]
$stmt->bind_param(“s”, $one);
$stmt->bind_param(“s”, $two);
$stmt->bind_param(“s”, $three);
[/code]

Or can I do something like this? I’m getting errors “mysqli_stmt::bind_param(): Number of variables doesn’t match number of parameters in prepared statement in” but following another example which suggests this is possible.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmOct 31.2021 — Not much help, but if you used the much easier to use PDO interface this would not occur since you don't have to specify a type with PDO. You simply specify the tag name (used in the query) and the variabe name in an array of args and use that for the execute. And in your partiular case, you don't have to specify all of the args in the array at one time.
Copy linkTweet thisAlerts:
@NogDogOct 31.2021 — Yeah, it's easier in PDO as you can just pass an array of [':place_holder' => 'value'] pairs directly to the execute(). But, you might be able to leverage call_user_func_array() (untested, no promises...PDO is definitely easier ;) ):
[code=php]
// Note: pass variables by reference:
$bindings = [
'dss',
&$id,
&$first_name,
&$last_name
];
if(!empty($middle_name)) {
$bindings[0] .= 's';
$bindings[] = &$middle_name
}
// where $stmt is your mysqli_stmt object:
call_user_func_array([$stmt, 'bind_param'], $bindings);
[/code]
×

Success!

Help @kiwis 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.25,
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,
)...