帮助将此 mysql 查询变成 PDO 查询

发布于 2024-10-26 11:01:25 字数 2131 浏览 2 评论 0原文

我已经使用它有一段时间了,但从那时起我升级到 PDO 而不是 mysql 查询。但我似乎根本无法让这个特定的片段发挥作用。

$query = mysql_query ($sql) or die (mysql_error());
while ($records = @mysql_fetch_array ($query)) {
    $alpha[$records['alpha']] += 1;
    ${$records['alpha']}[$records['id_clients']] = array(
        $records['first_name'],       //item[0]
    );
}

我已经尝试过了

while ($records = $this->db->fetch_row_assoc($sql)) {
    $alpha[$records['alpha']] += 1;
    ${$records['alpha']}[$records['id_clients']] = array(
        $records['first_name'],       //item[0]
    );
}

,我的 PDO 代码是

public function query($statement)
    {
        return self::$PDO->query($statement);
    }
public function fetch_row_assoc($statement) {
    self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        try
        {
            $stmt = self::$PDO->query($statement); 
            $result = $stmt->fetch();
            return $result;
        }catch(PDOException $e){
            echo $e->getMessage();
        }
        return false;
        //return self::$PDO->query($statement)->fetch(PDO::FETCH_ASSOC);

    }

查询函数有效,但我引入了 fetch_row_assoc 函数,希望我可以使用 mysql_fetch_asso 做同样的事情。

$sql = "SELECT 
                    SUBSTRING(`last_name`, 1, 1) AS alpha,
                    SUBSTRING(`middle_name`, 1, 1) AS subMiddleName,
                    `id_clients`,
                    `type`,
                    `first_name`,
                    `middle_name`,
                    `last_name`,
                    `address`,
                    `primary_number`,
                    `secondary_number`,
                    `home_number`,
                    `office_number`,
                    `cell_number`,
                    `fax_number`,
                    `ext_number`,
                    `other_number`,
                    `comments`
                FROM `clients`
                WHERE `user_id` = 1
                AND `is_sub` = 0
                AND `prospect` = 1
                ORDER BY `last_name`";

I have been using this for a while, but have since then upgraded to PDO instead of mysql queries. But I can't seem to get this particular snippet to work at all.

$query = mysql_query ($sql) or die (mysql_error());
while ($records = @mysql_fetch_array ($query)) {
    $alpha[$records['alpha']] += 1;
    ${$records['alpha']}[$records['id_clients']] = array(
        $records['first_name'],       //item[0]
    );
}

I have tried this

while ($records = $this->db->fetch_row_assoc($sql)) {
    $alpha[$records['alpha']] += 1;
    ${$records['alpha']}[$records['id_clients']] = array(
        $records['first_name'],       //item[0]
    );
}

and my PDO code is

public function query($statement)
    {
        return self::$PDO->query($statement);
    }
public function fetch_row_assoc($statement) {
    self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        try
        {
            $stmt = self::$PDO->query($statement); 
            $result = $stmt->fetch();
            return $result;
        }catch(PDOException $e){
            echo $e->getMessage();
        }
        return false;
        //return self::$PDO->query($statement)->fetch(PDO::FETCH_ASSOC);

    }

The query function works, but I have introduced my fetch_row_assoc function in hopes that I can do that same thing with mysql_fetch_asso.

$sql = "SELECT 
                    SUBSTRING(`last_name`, 1, 1) AS alpha,
                    SUBSTRING(`middle_name`, 1, 1) AS subMiddleName,
                    `id_clients`,
                    `type`,
                    `first_name`,
                    `middle_name`,
                    `last_name`,
                    `address`,
                    `primary_number`,
                    `secondary_number`,
                    `home_number`,
                    `office_number`,
                    `cell_number`,
                    `fax_number`,
                    `ext_number`,
                    `other_number`,
                    `comments`
                FROM `clients`
                WHERE `user_id` = 1
                AND `is_sub` = 0
                AND `prospect` = 1
                ORDER BY `last_name`";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-11-02 11:01:25

试试这个:

public function query($statement)
    {
        return self::$PDO->query($statement);
    }
public function fetch_row_assoc($statement) {
    self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        try
        {
            $stmt = self::$PDO->query($statement); 
            $stmt->setFetchMode(PDO::FETCH_ASSOC);
            $result = $stmt->fetch();
            return $result;
        }catch(PDOException $e){
            echo $e->getMessage();
        }
        return false;
    }

Try this:

public function query($statement)
    {
        return self::$PDO->query($statement);
    }
public function fetch_row_assoc($statement) {
    self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        try
        {
            $stmt = self::$PDO->query($statement); 
            $stmt->setFetchMode(PDO::FETCH_ASSOC);
            $result = $stmt->fetch();
            return $result;
        }catch(PDOException $e){
            echo $e->getMessage();
        }
        return false;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文