Doubt on white space within PHP code

1

I am connecting to prepared statments to insert many variables, and in some cases it takes a long time to find the errors, and now I packed in one here and hit me a doubt:

  

Blanks make a difference, I can break lines and give   spaces quietly?

For example, I can do:

mysqli_stmt_bind_param($stmt,
    'sisss
    siiii
    iissi

Or it has to be:

mysqli_stmt_bind_param($stmt, 'sissssiiiiiissi ....

Same for variables and field declaration. I'm putting 3 in 3 in each line for everything, to make it easier to count and to visualize the lack of some variable etc ...

$sql = "INSERT INTO tabela(
campo1, campo2, campo3,
campo4, campo5, campo6 ....

Only the values that putting 10 out of 10:

VALUES
(
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ....

In the case of the error in question, I checked it a million times, so I know that nothing is missing, everything has 138oooo !! (: /) So I think there must be some mistake in the indentation, because there are spaces and breaks of too many lines ...

The error is as follows:

  

"Number of elements in type definition string does not match number of   bind variables "

Is there any way to know at least what is the difference, if one of a field or 20?)

So here in the case the question is: can the blanks be affecting the code? How does PHP interpret white space within the script (I searched, but I only find explanations about number of bind variables :): /)?

    
asked by anonymous 03.10.2015 / 07:16

2 answers

1

Spaces or line breaks are interpreted by MYSQLi as one of the types of placeholder the error generated is:

  

Number of elements in type definition string does not match number of bind variables in

Anything other than the types defined in documentation sidb generates the error, x is invalid character and n is position +1.

  

Undefined fieldtype x (parameter n)

Error simulation

CREATE TABLE 'pessoas' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'nome' varchar(45) DEFAULT NULL,
  'idade' int(11) DEFAULT NULL,
  'profissao' varchar(45) DEFAULT NULL,
  'data' timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY ('id'),
  UNIQUE KEY 'id_UNIQUE' ('id')
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8

Test file

<?php

$nome = 'teste '. rand(0, 5000);
$idade = rand(1,120);
$profissao = 'pro';


$db = new mysqli('localhost', 'root', 'root', 'web');
$sql = "INSERT INTO pessoas (nome, idade, profissao) VALUES(?,?,?)";
$stmt = $db->prepare($sql);

//quebra de linha proposital
$stmt->bind_param('s
ss', $nome, $idade, $profissao);

if(!$stmt->execute()){
    echo '<br>Erro: SQLState:'. $db->errno . ' Descrição: '. $db->error;
}else{
    echo 'sucesso';
}

The output of the above test and the variant assumes that four types have been defined and only three values (variables) are passed.

$stmt->bind_param('s ss', $nome, $idade, $profissao);
--------------------^ 

is the same:

  

Number of elements in type definition string does not match number of bind variables in

Other curious examples

$stmt->bind_param('aaa', $nome, $idade, $profissao);
$stmt->bind_param('   ', $nome, $idade, $profissao);

Output:

  

Undefined fieldtype a (parameter 2 ) in

Solutions

To keep the type definition organized, I suggest you set a pattern and assign it to a variable and remove the invalid characters with str_replace() .

$invalidos = array("\n","\r", "\r\n", " ");
$tipos ='s
s
s';

$tipos = str_replace($invalidos, "",$tipos);
$stmt->bind_param($tipos, $nome, $idade, $profissao);

The insert is performed without problems. Another simple solution is to pass the number of parameters to str_repeat() do the work, in the example I purposely used all the values as s I believe that this typing is not good at 90% or the use of them is not obvious at all .

Workaround

$tipos = str_repeat('s', 3);
$stmt->bind_param($tipos, $nome, $idade, $profissao);
    
03.10.2015 / 19:22
2

In PHP , you can break spaces in some functions, there is no problem, for example in an array :

$array = array(
              'indice'=>                     'Valor',
              'indice1'                 => 'Valor' 
              );

In this function for example, you can place spaces, as long as the values in it keep the structure, it continues to work, and without any problems.

But in other cases, not even that. Let's see for example when it comes to Strings , Integers , and others, you will usually get a return error, see this example:

03.10.2015 / 17:52