/    Sign up×
Community /Pin to ProfileBookmark

Warning: mysqli_stmt_bind_param() expects parameter 2 to be string, array given

I have some code which I don’t fully understand how it is working. It achieves the desired result but I keep getting warnings: `mysqli_stmt_bind_param() expects parameter 2 to be a string`

I think the line `mysqli_stmt_bind_param( $stmt, $bind_params );` is superfluous but dont want to just go deleting it without understanding the implications.

“`
if ( $stmt = mysqli_prepare($this->mysqli, $query) ) :

$customer_id = $this->customer[“id”];
$invoice_no = $dataArrayRow[‘Invoice No’];
$sale_type_desc = $dataArrayRow[‘Sale Type Desc’];
$file_date = $this->fileGeneratedDate->format(‘Y-m-d H:i:s’);

$bind_params = array(‘isss’, &$customer_id, &$invoice_no, &$sale_type_desc, &$file_date);

call_user_func_array( array($stmt, ‘bind_param’), $bind_params);
mysqli_stmt_bind_param( $stmt, $bind_params );

// Execute the statement
if ( !mysqli_stmt_execute( $stmt ) ) :

endif;
endif;
“`

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 25.2021 — Having not used the mysqli interface and looking this extremely hard to follow example in your code, I am so glad I have only used PDO.

If your host (or you?) has the PDO interface available, I heartily recommend it. Much, much simpler to use.

Here's a quick sample of how a simple query can be handled:
<i>
</i>$q = "select fld1, fld2, fld3,...... from tablename where fld1=:parm1 and fld2 = :parm2";
$qst = $pdo-&gt;prepare($q);
$parms = array('parm1'=&gt;$arg1, 'parm2'=&gt;$arg2);
if (!$qst-&gt;execute($parms))
{ handle error results}
// if query runs
while ($row = $qst-&gt;fetch(PDO::FETCH_ASSOC))
{
(handle the rows one at a time)
}


Pretty simple looking, isn't it?
Copy linkTweet thisAlerts:
@NogDogAug 25.2021 — Looks like the call_user_func_array() line right before it already is doing the binding. In effect it is doing:
[code=php]
$stmt->bind_param('isss', &$customer_id, &$invoice_no, &$sale_type_desc, &$file_date);
[/code]

So yeah, does not appear that you need the mysqli_stmt_bind_param() call, though if you did want to use the procedural version of it like that, you would have to spell out all those arguments:
[code=php]
mysqli_stmt_bind_param($stmt, 'isss', &$customer_id, &$invoice_no, &$sale_type_desc, &$file_date);
[/code]
Copy linkTweet thisAlerts:
@php-bgraderauthorSep 11.2021 — Yes, you are correct. Have been playing with and reading up on this over the past 2 weeks.

mysqli_stmt_bind_param() is usually all you need however, there was a requirement to update different fields depending on a user type. Therefore, call_user_func_array( array($stmt, 'bind_param'), $bind_params); allowed me to dynamically assign the parameters depending on who was updating (and therefore, which fields were being updated.)

I obviously copied and pasted this code and re-purposed without fully understanding what it was doing.

For 99% of cases all that is required is:

``<i>
</i>if ( $stmt = mysqli_prepare($this-&gt;mysqli, $query) ) :

$customer_id = $this-&gt;customer["id"];
$invoice_no = $dataArrayRow['Invoice No'];
$sale_type_desc = $dataArrayRow['Sale Type Desc'];
$file_date = $this-&gt;fileGeneratedDate-&gt;format('Y-m-d H:i:s');

mysqli_stmt_bind_param('isss', $customer_id, $invoice_no, $sale_type_desc, $file_date);

// Execute the statement
if ( !mysqli_stmt_execute( $stmt ) ) :
...
endif;
endif;<i>
</i>
``
×

Success!

Help @php-bgrader 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.29,
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,
)...