栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将多个参数绑定到mysqli查询中

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将多个参数绑定到mysqli查询中

不幸的是,默认情况下,bind_param()不接受数组而不是单独的变量。但是,自PHP 5.6起,将进行重大改进。

要将任意数量的变量绑定到mysqli查询中,您将需要一个参数解包运算符。这将使操作尽可能简单和流畅。

例如,要将PHP数组与mysql的

IN()
运算符一起使用,您将需要以下代码

// our array$words = ['a','b','c'];// create an SQL query with placeholders and prepare it$in    = str_repeat('?,', count($array) - 1) . '?'; //  returns ?,?,?...$sql   = "SELECt name FROM table WHERe city IN ($in)"; $stmt  = $mysqli->prepare($sql);// create the types string dynamically and bind an array$types = str_repeat('s', count($array)); // returns sss...$stmt->bind_param($types, ...$array);// execute and fetch the rows$stmt->execute();$result = $stmt->get_result(); // get the mysqli result$data = $result->fetch_all(MYSQLI_ASSOC); // fetch the data


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号