php如何去封装mysqli_prepare?

WBOY
Release: 2016-06-06 20:16:24
Original
1495 people have browsed it

我想把PHP的mysqli_prepare及其查询过程中需要用到的方法封装到我自己写的类里面去,但是遇到这个问题:
我写的query()函数接收sql,charset,params参数,其中sql参数使用占位符以便进行预处理,params参数是一个数组,是用来绑定占位符的参数的的数组。
那么问题来了,占位符与参数的绑定是利用mysqli_stmt::bind_param()函数去绑定的,官方的文档说是这样使用的:

prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
?> 
Copy after login
Copy after login

我不知道我接收的数组的长度(即使用不定参数也不知道),那我要怎样才能绑定参数呢?
如果用if或者switch去判断,然后根据相应长度去调用的话,会不会太麻烦,而且不可能包含所有的情况。
所以应该怎样去封装预处理查询的一系列方法呢?

回复内容:

我想把PHP的mysqli_prepare及其查询过程中需要用到的方法封装到我自己写的类里面去,但是遇到这个问题:
我写的query()函数接收sql,charset,params参数,其中sql参数使用占位符以便进行预处理,params参数是一个数组,是用来绑定占位符的参数的的数组。
那么问题来了,占位符与参数的绑定是利用mysqli_stmt::bind_param()函数去绑定的,官方的文档说是这样使用的:

prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
?> 
Copy after login
Copy after login

我不知道我接收的数组的长度(即使用不定参数也不知道),那我要怎样才能绑定参数呢?
如果用if或者switch去判断,然后根据相应长度去调用的话,会不会太麻烦,而且不可能包含所有的情况。
所以应该怎样去封装预处理查询的一系列方法呢?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!