简体   繁体   中英

How to set PHP Prepared Statement param to output of mysql function return

In below example 3rd parameter is MySQL function

generateProductCode(".$obj->a.")

is function which will be returning string as output after execution in MySQL. How to bind MySQL function output after execution as parameter in prepared statement?

$sql = "INSERT INTO product(`a`, `b`, `c`) as ( ?, ?, ?);"
$types = "ids";
$params = [$obj->a, $obj->b , "generateProductCode(".$obj->a.")"];

I know, we can do that by running SQL separately. With normal sql query below query works

"INSERT INTO product(`a`, `b`, `c`) as (".$obj->a.",".$obj->b.",generateProductCode(".$obj->a."))";

You use your generateProductCode function call inside your query and provide the parameter as usual with the ? marker.

$sql = "INSERT INTO product(a, b, c) as ( ?, ?, generateProductCode(?));"
$params = [$obj->a, $obj->b , $obj->a];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM