問題描述
我是 php 的新手,正在努力學習它為什么你們和女孩需要原諒我問了很多!
I am new to php and trying hard to learn its why you guys and gals need to Forgive me for asking a lot!
這是我的問題;
我試圖多次調用帶有 where 子句的函數,我已經閱讀了幾乎所有的帖子和示例,但仍然不明白該怎么做.
I am trying to call a function with where clause multiple times, I have read allmost all posts and examples still didn't understand how to do it.
我認為一個例子比我能寫的任何簡介都更有用.
I tought that An example will be more useful than any blurb I can write.
這是我嘗試創建并多次使用的函數:
Here is the function I am trying to create and use it multiple times :
function getTable($tableName, $clause) {
$stmt = $pdo->prepare("SELECT * FROM ".$tableName." WHERE ".$clause." = :".$clause);
$stmt->bindParam(":$clause", $clause, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() > 0){
return true;
}else{
return false;
}
return $stmt;
}
我不確定我的功能是否安全或正確.
I am not sure if my fucntion is safe or its rigth.
這就是我試圖調用函數的方式,我不知道如何調用表名和 where 子句以及如何打開 while 循環.
AND this is how I am trying to call function, which I dont know how to call table name and where clause and how to turn while loop.
getTable('posts');
如果你能給出一個創建和調用函數的例子,我將不勝感激,謝謝
If you give an example of creating and caling function, I would be grateful, Thanks
推薦答案
不,你的函數不安全.此外,它只是無用的.沒有任何用例可以像這樣使用它 getTable('posts');
.對于其他一切,最好允許完整的 SQL 語法,而不是某些有限的子集.
Nope, your function is not safe. Moreover it is just useless. There is no use case where you would use it like this getTable('posts');
. And for the everything else it is much better to allow the full SQL syntax, not some limited subset.
我能想到的最簡單但最強大的 PDO 函數是一個接受 PDO 對象、SQL 查詢和帶有輸入變量的數組的函數.返回 PDO 語句.我在關于 PDO 輔助函數的文章中寫到了這樣的函數.所以這是代碼:
The simplest yet most powerful PDO function I can think of is a function that accepts a PDO object, an SQL query, and array with input variables. A PDO statement is returned. I wrote about such function in my article about PDO helper functions. So here is the code:
function pdo($pdo, $sql, $args = NULL)
{
if (!$args)
{
return $pdo->query($sql);
}
$stmt = $pdo->prepare($sql);
$stmt->execute($args);
return $stmt;
}
使用此功能,您將能夠使用任意數量的 WHERE 條件運行任何查詢,并獲得多種不同格式的結果.以下是上述文章中的一些示例:
With this function you will be able to run any query, with any number of WHERE conditions, and get results in many different formats. Here are some examples from the article mentioned above:
// getting the number of rows in the table
$count = pdo($pdo, "SELECT count(*) FROM users")->fetchColumn();
// the user data based on email
$user = pdo($pdo, "SELECT * FROM users WHERE email=?", [$email])->fetch();
// getting many rows from the table
$data = pdo($pdo, "SELECT * FROM users WHERE salary > ?", [$salary])->fetchAll();
// getting the number of affected rows from DELETE/UPDATE/INSERT
$deleted = pdo($pdo, "DELETE FROM users WHERE id=?", [$id])->rowCount();
// insert
pdo($pdo, "INSERT INTO users VALUES (null, ?,?,?)", [$name, $email, $password]);
// named placeholders are also welcome though I find them a bit too verbose
pdo($pdo, "UPDATE users SET name=:name WHERE id=:id", ['id'=>$id, 'name'=>$name]);
// using a sophisticated fetch mode, indexing the returned array by id
$indexed = pdo($pdo, "SELECT id, name FROM users")->fetchAll(PDO::FETCH_KEY_PAIR);
特別適合你,這里是 while
的例子,雖然這個方法被認為是笨拙和過時的:
Special for you, here is the while
example, though this method is considered clumsy and outdated:
$stmt = pdo($pdo,"SELECT * FROM tableName WHERE field = ?",[$value]);
while ($row = $stmt->fetch()) {
echo $row['name'];
}
這篇關于如何在php pdo中使用where子句多次調用函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!