这就是我通常用数据库信息运行循环的方法
$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 }更多推荐
发布评论