PDO(prepare) analyzes number of characters?

Asked

Viewed 33 times

1

My question is simple: Imagine I have a table Nome and in it an attribute nome which is a varchar(10). If I use the command $preparado = $con->prepare("INSERT INTO Nome(nome) VALUES('euTenhoMaisDeDezLetras')"); will return some error by exceeding the limit? what would happen if I did $preparado->execute();?

  • 1

    it will insert only the first 10 characters and the rest of the word will not be inserted

  • You are losing 90% of prepare utility when not performing variable Binding in query...

  • i didn’t use bindParam() for example get faster to understand

1 answer

0


Varchar by default has the variable value, i.e., occupying only that which is inserted. As you can see, the corresponding number within varchar(n) is the amount of characters supported, it is essential that you handle before entering the information into the database, since the maximum set by you is 10. I recommend using the php function strlen().

More about the size of the varchar Varchar

More about strlen

<?php

$str = 'euTenhoMaisDeDezLetras';

if (strlen($str) <= 10) {
    // todo o código anterior
    $preparado->execute();
} else {
    // tratar caso for maior que 10
}
  • Thanks my doubt was more about whether it was necessary to treat, or whether to prepare it already did it for me

  • 1

    Any and all information is essential that you, regardless of size, beyond the type of data you are entering, even because you do not want an error for a user trying to insert a date in an int field is not even, strong hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.