Thinking about a bank system.
If I have a $ balance variable; , I can throw a string inside that variable in PHP.
I would like to know how to solve this problem in php.
And what is the advantage of php having dynamic variable type.
Thinking about a bank system.
If I have a $ balance variable; , I can throw a string inside that variable in PHP.
I would like to know how to solve this problem in php.
And what is the advantage of php having dynamic variable type.
PHP is dynamic, but even if it was not, it does not really define what will go into the database.
In PHP it is possible to control the input data, except that the data that comes from the database has the type of the "approximate" value of the database (except some that have seen string, for example BIGINT
and DECIMAL
), or when PHP is 32bit and 64bit mysql (which affects some "boundaries"), this may depend on the API you are using (mysqli or pdo), I am not sure.
For example if you do this by reading data:
<?php
//Conecta
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
//Verifica se houve erros
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit;
}
//Query
$query = "SELECT id, nome, preco FROM Tabela LIMIT 1";
//Executa query
if ($result = $mysqli->query($query)) {
/* Pega os dados */
while ($row = $result->fetch_assoc())
{
//Exibe na tela, semelhante ao Symtem.out.println
var_dump($row);
}
/* limpa resultados */
$result->free();
}
/* fecha conexão */
$mysqli->close();
It returns something like this:
array(3) { ["id"]=> int(1) ["nome"]=> string(8) "carrinho" ["preco"]=> string(6) "100.02" }
id
is INT
and returned a type int
nome
is VARCHAR
and returned string
preco
is DECIMAL
and returned string
In case probably DECIMAL turns string because if it turned
float
could lose the accuracy of the data, which would be a big headache
See that even without typing the value is of a specific type in case the ID is still INT (as I said I'm not sure of the situation for different processors and APIs)
However it is possible to pass data to query so that it checks the input data:
<?php
$stmt = $mysqli->prepare("INSERT INTO Tabela VALUES (?, ?, ?, ?)");
$stmt->bind_param('isd', $id, $nome, $valor);
$id = 2;
$nome = 'Boneca';
$valor = 11.2;
/* execute prepared statement */
$stmt->execute();
printf("%d linhas inseridas\n", $stmt->affected_rows);
/* close statement and connection */
$stmt->close();
/* close connection */
$mysqli->close();
See:
int
double
string
Although it is a question of DECIMAL
, it would be better to pass the same string.
!!! I recommend you read this: Best data type to work with money? !!!
mysql itself does "CAST" when it works on a column of a specific type, this is independent of any language, so whether the language is dynamic or not irrelevant in the end.
This is just an addendum, there are several ORMs in PHP, such as Propel , Doctrine and frameworks that have their own ORM systems, for example Laravel , they can control it through Migrations
the types of column values