0

I have the following PHP variable:

$qStr1;

This, when printed or echoed, shows the following:

$q1, $q2, $q3, $q4, $q5

I have a bind-parm code:

mysqli_stmt_bind_param($proc, "iss$is", $respondent_id, $ip, $browser, $q1, $q2, $q3, $q4, $q5);

This works fine and saves correctly to the database, however, when I replace the individual parts with the variable (see below), it doesn't work, what am I doing wrong?

mysqli_stmt_bind_param($proc, "iss$is", $respondent_id, $ip, $browser, $qStr1);

Vardump is as follows:

string(49) "$q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, $q9, $q10"
Dharman
  • 30,962
  • 25
  • 85
  • 135
Homer_J
  • 3,277
  • 12
  • 45
  • 65

4 Answers4

1

It doesn't work like that. The method wants several parameters. No you give it one, which holds a string.

There are possibilities to do this, like eval, but I advise you to look for a different approach. PDO for example can bind using names which can be called in a loop.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Yes, I've gathered as much - this is getting beyond my experience and was hoping for some guidance. – Homer_J Jul 25 '13 at 13:47
  • The quick and dirty way would be to use `eval`, the "good" way is really to go back a few steps and see if you really need what you think you need. – Bart Friederichs Jul 25 '13 at 13:48
  • It might help if you could explain why you want to do it like this. I can't see anything wrong in adding those 10 variables to the bind function call, but maybe you have your reasons for this. – mgrueter Jul 25 '13 at 13:49
  • I need to be able to programatically do this as I have upwards of 10 pages with circa 10 to 50 questions on each - without it done like this I have manually code each page. – Homer_J Jul 25 '13 at 13:49
  • As above - the reason is that I could have upwards of 50 questions on a page and didn't want to have to do it manually. – Homer_J Jul 25 '13 at 13:55
0

As it says in the manual

The number of variables and length of string types must match the parameters in the statement.

That function expects x parameters to match the number of statement parameters and has not been written to expand one parameter into many.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

$qStr1 is a string which contains the names of the variables, not the content of the variables. It just doesn't work that way.

mgrueter
  • 1,400
  • 6
  • 15
0

From the var_dump, you can see that $qStr1 is just a string with all the variable names (not values) inside it.

If you're using single quotes for defining the variables, this will happen. So, if you want $qStr1 to have the actual variables' value, then you'll have to enclose it in double quotes:

Example:

<?php

$q1 = '1'; 
$q2 = '1';
$q3 = '1';
$q4 = '1';
$q5 = '1';

$qStr1 = '$q1, $q2, $q3, $q4, $q5';
var_dump($qStr1);

$qStr1 = "$q1, $q2, $q3, $q4, $q5";
var_dump($qStr1);

Output:

string(23) "$q1, $q2, $q3, $q4, $q5"
string(13) "1, 1, 1, 1, 1"

Hope this helps!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • I don't think this is it - $q1, $q2 etc. are the variables I need. They are currently stored in a string but I need it to look like this for the purposes of saving to the db. `mysqli_stmt_bind_param($proc, "iss$is", $respondent_id, $ip, $browser, $q1, $q2, $q3, $q4, $q5);` – Homer_J Jul 25 '13 at 13:51
  • Appreciate the update but no - this isn't what I am looking for. I am trying to find a way to `loop` or something `mysqli_stmt_bind_param($proc, "iss$is", $respondent_id, $ip, $browser, $q1, $q2, $q3, $q4, $q5);` so I don't have to manually code thr $q1, $q2 etc. – Homer_J Jul 25 '13 at 13:54
  • If I understand you correctly, this is what you need: `echo "mysqli_stmt_bind_param($proc, \"iss$is\", $respondent_id, $ip, $browser, $qStr1)";` – Amal Murali Jul 25 '13 at 13:57
  • That is indeed what I have but it doesn't work as $qStr1 needs to be replaced by its actual contents to make the bind-param work. – Homer_J Jul 25 '13 at 14:02