-2

这是实现功能之前的代码,

try {
    $conn = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
    $stmt = $conn->prepare('SELECT * FROM posts WHERE status= :published ORDER BY id DESC LIMIT 5');
    $stmt->execute(array(':published' => 'published'));
    while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $contents = $result['content'];
    $title = $result['title']; 
.... 

这很好用。然后我将 db 连接命令移动到单独的 php 文件(functions.php)。并创建了这个功能,

function run_db($sqlcom,$exe){
    $conn = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare($sqlcom);
    $stmt->execute($exe);
    $data_db = $stmt->fetch(PDO::FETCH_ASSOC) ;
    return $data_db;
}

然后我像这样更改了第一个提到的代码,

try {
    $data_db=run_db('SELECT * FROM posts WHERE status= :published ORDER BY id DESC LIMIT 5',array(':published' => 'published'));   
    while ($result = $data_db) {
    $contents = $result['content'];
    $title = $result['title']; 

然后我得到的只是一个无限重复的帖子。谁能告诉我如何纠正这个问题?

4

4 回答 4

3

把函数改成这样:

function run_db($sqlcom,$exe){
    $conn = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare($sqlcom);
    $stmt->execute($exe);
    return $stmt;
}

以及对该函数的调用:

try {
    $stmt = run_db('SELECT * FROM posts WHERE status= :published ORDER BY id DESC LIMIT 5',array(':published' => 'published'));   
    while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $contents = $result['content'];
        $title = $result['title'];

编辑:更好的解决方案是jeroen建议的解决方案 -一次返回所有获取的对象

function run_db($sqlcom,$exe){
    $conn = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare($sqlcom);
    $stmt->execute($exe);
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

然后这样调用:

try {
    $data = run_db('SELECT * FROM posts WHERE status= :published ORDER BY id DESC LIMIT 5',array(':published' => 'published'));   
    foreach($data as $result) {
        $contents = $result['content'];
        $title = $result['title'];

编辑2:无论如何-将这样的逻辑包装到一个函数中并不是一个好主意。现在您只能执行SELECT查询,结果数组始终只包含记录的关联数组。如果您想(出于任何原因)检索对象数组,甚至只检索一个值怎么办?如果您想执行INSERT, UPDATE,DELETE查询怎么办???

如果您确定要这样做,那么我想创建一个具有以下功能的类:

class MyPDO {
    private $connection;

    static $instance;

    function __construct() {
        $this->connection = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass');
        $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    static function getInstance() {
        return self::$instance ?  : self::$instance = new MyPDO;
    }

    // retrieves array of associative arrays
    function getAssoc($sqlcom, $exe) {
        $stmt = $this->connection->prepare($sqlcom);
        $stmt->execute($exe);

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // retrieves array of objects
    function getObj($sqlcom, $exe) {
        $stmt = $conn->prepare($sqlcom);
        $stmt->execute($exe);

        return $stmt->fetchAll(PDO::FETCH_OBJ);
    }

    // retireves one single value, like for SELECT 1 FROM table WHERE column = true
    function getOne($sqlcom, $exe) {
        $stmt = $conn->prepare($sqlcom);
        $stmt->execute($exe);

        return $stmt->fetchColumn();
    }

    // just executes the query, for INSERT, UPDATE, DELETE, CREATE ...
    function exec($sqlcom, $exe){
        $stmt = $conn->prepare($sqlcom);

        return $stmt->execute($exe);
    }
}

然后你可以这样称呼它:

try {
    $pdo = MyPDO::getInstance();
    foreach($pdo->getAssoc('MySQL QUERY'), array($param, $param)) as $result) {
        print_r($result);
    }
} catch(\Exception $e) {
    // ...
}
于 2013-05-17T11:47:07.860 回答
2

只需返回语句:

function run_db($sqlcom,$exe){
    static $conn;
    if ($conn == NULL)
    {
        $conn = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass');
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    $stmt = $conn->prepare($sqlcom);
    $stmt->execute($exe);
    return $stmt;
}


try {
    $stmt=run_db('SELECT * FROM posts WHERE status= :published ORDER BY id DESC LIMIT 5',array(':published' => 'published'));   
    while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $contents = $result['content'];
    $title = $result['title']; 

您还可以在连接上设置默认的 fecth 模式。

于 2013-05-17T11:47:31.997 回答
0

$stmt从函数返回的正确答案的替代方法是获取函数中的所有行foreach并在主代码中使用 a :

在功能上:

...
$data_db = $stmt->fetchAll(PDO::FETCH_ASSOC) ;
return $data_db;

函数外:

$data_db=run_db('SELECT * FROM posts WHERE status= :published ORDER BY id DESC LIMIT 5',array(':published' => 'published'));   
foreach ($data_db as $result) {
  $contents = $result['content'];
  $title = $result['title']; 
  ...
于 2013-05-17T11:51:52.500 回答
0

您的代码有两个基本问题。

  1. 每次运行查询时都会连接。
  2. 它只返回一种格式的数据,而 PDO 可以返回几十种不同格式的结果。

接受的答案是错误的,因为它只是试图重新发明 PDO 功能,但方式非常肮脏,有很多重复的代码,但仍然无法使其与普通 PDO 一样好。

正如 xdazz 的回答中所说,您必须返回声明。然后使用 PDO 的本机获取模式,使用方法链获取所需格式的结果。

此外,您不应该添加try到您的代码中。

于 2015-10-03T07:07:40.630 回答