从函数返回关联数组(Return associative array from function)

编程入门 行业动态 更新时间:2024-10-24 10:26:47
从函数返回关联数组(Return associative array from function)

这就是我通常用数据库信息运行循环的方法

$query = "SELECT * from courses ORDER BY id DESC LIMIT 4"; $stmt = $db->prepare($query); $stmt->execute(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { }

现在$row[]可用于表示循环内的数据库信息。

我的问题是,我会在单独的while循环中大量使用这个查询。 但是,我不希望每次需要时都要完成此语句,因此我希望函数能够引用所需的任何变量。

例如,我试过了。

function retrieve_assoc_array_limit4($table) { $user = '***'; $pass = '***'; $db = new PDO('mysql:host=localhost;dbname=***', $user, $pass); $query = ("SELECT * FROM ? ORDER BY id DESC LIMIT 4"); $stmt = $db->prepare($query); $stmt->bindValue(?, $table); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); return $row; }

但是当我试图在它的同时获得无限量的相同信息的回报时。

while($row = retrieve_assoc_array_limit4($table_name)) { exho $data; }

我的问题是如何在逻辑上使用函数中的第一个代码,这样我就可以在不使用完整语句的情况下使用它。

This is how I would normally run a loop with database information

$query = "SELECT * from courses ORDER BY id DESC LIMIT 4"; $stmt = $db->prepare($query); $stmt->execute(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { }

Now $row[] can be used to represent database information within the loop.

My problem is that I'll be utilizing this query a lot for separate while loops. However, I don't wish to have to complete this statement every time I require it, so I want a function to be able to refer to with any variables required.

For instance, I tried.

function retrieve_assoc_array_limit4($table) { $user = '***'; $pass = '***'; $db = new PDO('mysql:host=localhost;dbname=***', $user, $pass); $query = ("SELECT * FROM ? ORDER BY id DESC LIMIT 4"); $stmt = $db->prepare($query); $stmt->bindValue(?, $table); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); return $row; }

But when trying to while it I get an unlimited amount of returns of the same information whiled.

while($row = retrieve_assoc_array_limit4($table_name)) { exho $data; }

My question is how logically to use the first code in a function so that I can use it in the same with without writing out the full statement relentlessly.

最满意答案

许多方法,使用您的代码几乎没有变化:

调用fetchAll()而不是fetch():

function retrieve_assoc_array_limit4($table) { $user = '***'; $pass = '***'; $db = new PDO('mysql:host=localhost;dbname=***', $user, $pass); $query = ("SELECT * FROM ? ORDER BY id DESC LIMIT 4"); $stmt = $db->prepare($query); $stmt->bindValue(?, $table); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); return $rows; }

然后打电话给你的功能,做一会儿或做一些事情:

$rows = retrieve_assoc_array_limit4($table_name); foreach($rows as $row){ //do your stuff }

many ways of doing this, to use your code with little changes:

calling fetchAll() instead of fetch():

function retrieve_assoc_array_limit4($table) { $user = '***'; $pass = '***'; $db = new PDO('mysql:host=localhost;dbname=***', $user, $pass); $query = ("SELECT * FROM ? ORDER BY id DESC LIMIT 4"); $stmt = $db->prepare($query); $stmt->bindValue(?, $table); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); return $rows; }

then call your function and do a while or foreach:

$rows = retrieve_assoc_array_limit4($table_name); foreach($rows as $row){ //do your stuff }

更多推荐

本文发布于:2023-08-07 02:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   函数   Return   function   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!