在 php mysqli 准备好的语句中将数据附加到数组中
我很累在这里写一个简单的函数。这个想法是能够调用函数并显示数据,但我想知道是否有一种方法可以将其填充到某种数组或其他东西中,以便我可以在调用函数时设置结果的样式。函数如下:
function getDBH() {
static $DBH = null;
if (is_null($DBH)) {
$DBH = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
}
return $DBH;
}
function selectInfo($limit, $offset){
$DBH = getDBH();
$stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
$stmt->bind_param("ii", $limit, $offset);
$stmt->execute();
$stmt->bind_result($id, $name, $email);
}
I am tyring to write a simple function here. The idea is to be able to call the function and display the data, but i am wondering if there is a way to stuff it into an array of some sort or something so that i can style the results when the function is called. the function is as follows:
function getDBH() {
static $DBH = null;
if (is_null($DBH)) {
$DBH = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
}
return $DBH;
}
function selectInfo($limit, $offset){
$DBH = getDBH();
$stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
$stmt->bind_param("ii", $limit, $offset);
$stmt->execute();
$stmt->bind_result($id, $name, $email);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
selectInfo()
函数的替换应返回一个多维数组,其中每条记录包含一个关联数组。它不依赖于您事先知道语句中返回哪些字段,因为如果您以任何方式更改表,则 select 语句中的*
很容易破坏内容。编辑:稍微更改了您的
selectInfo()
函数,因为我之前的答案太草率了。print_r(selectInfo(0,10));
将打印出字段名称完整的所有记录。This replacement for your
selectInfo()
function should return a multi-dimensional array containing one associative array per record. It does not rely on you knowing before hand what fields are returned in the statement as that*
in the select statement can otherwise easily break things if you alter your table in any way.Edit: Changed up your
selectInfo()
function a bit as my previous answer was just too sloppy.print_r(selectInfo(0,10));
will print out all records with field names intact.使用 !而不是?附加静态数据。 ?附加引用的转义数据。 !不安全,但不限。
Use ! instead of ? to attach static data. ? attaches quoted, escaped data. ! is unsafe, but not for limit.