如何使用php pdo中的where子句多次调用函数? [英] how to call a function multiple times with where clause in php pdo?

查看:100
本文介绍了如何使用php pdo中的where子句多次调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆