我是 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子句多次调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!