1

I have a number of similar ( but not identical ) sql statements. The parameter types vary for each call and so I want to pass these as variables. Something like this:

$stmt->bind_param( $typeString, $parameter1, $parameter1 );

Where $typeString can be set to 'ss' or 'is' or 'ds'.

I get the error Number of elements in type definition string doesn't match number of bind variables.

I have tried (for instance)

$typeString = "is";
$typeString = "\'".$typeString."\'";
$stmt->bind_param( $typeString, $parameter1, $parameter1 );

And

$typeString = "is";
$stmt->bind_param( $typeString, $parameter1, $parameter1 );

But still get an error.

Is it possible to set this with a variable?

7
  • 1
    Where is the sql statment? And never bind a variable twice, you use $parameter1 twice. Commented Mar 6, 2017 at 13:14
  • And you just only once bind: stackoverflow.com/questions/15748254/… then refill the parameter variables and execute again. And if possible use always s for param typ. Commented Mar 6, 2017 at 13:16
  • number of similar ( but not identical ) sql statements so if you have 2 sql statements then you have also 2 mysqli stmt objects (for each stmt one). So nothing should mixed up here. Commented Mar 6, 2017 at 13:19
  • Not sure I get it. Do you want to reuse a prepared statement for different queries? Commented Mar 6, 2017 at 13:19
  • Nope here: reuse of bounded varaibles for different execution of the query. And stmts are for multi execution made! Send it once to the sql server (query with placeholder), but execute it with diff parameter (only varialbes send to mysql). Commented Mar 6, 2017 at 13:21

1 Answer 1

1
  1. First variant makes no sense as quotes shouldn't be sent into type definition list.
  2. Second variant actually works.
  3. There is little use in setting specific parameters. Just use 's' for everything.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.