Reputation: 127
Never seen this before. Was unable to find and explanation and I saw it the other day in a code, e.g.:
$statement = $db->prepare("insert into table (col1, col2, col3) values (?, ?, ?)");
$data = array((string) $var1, (string) $var2, (string) $var3);
$statement->execute($data);
See this (string) part? Anyone to shed some light on it, please?
Upvotes: 0
Views: 245
Reputation: 677
You can bind parameter
$stmt = $conn->prepare("INSERT INTO table (col1, col2, col3) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $var1, $var2, $var3);
$stmt->execute();
Type specification chars
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
Upvotes: 0
Reputation: 3724
This is a simple cast , here in the code , while the query is a string , so the (string) make sure that you are manipulating String , here is the PHP DOC:
(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object
(unset) - cast to NULL
http://php.net/manual/en/language.types.type-juggling.php
Example:
$int = 4;
$string = (string)$int;
echo gettype($int); // output snteger
echo gettype($string); // output string
In general , the meaning of type casting is to use the value of a variable with different data type.
Upvotes: 1
Reputation: 66
It means everything that is written in $var1 and the others is converted to a String so the Programm can handle the input without getting an error.
Upvotes: 1