我用这段代码得到了奇怪的结果。 foreach 循环没有完成工作,而 while 循环超时。使用 PDO 进行查询

发布于 2024-10-27 00:50:26 字数 4966 浏览 1 评论 0原文

这是我的代码

public function alpha()
{
    $alpha = NULL;
    $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`";

//$query = mysql_query ($sql) or die (mysql_error());
//echo "test";
foreach($this->db->fetch_row_assoc($sql) as $records) {
//while ($records = $this->db->fetch_row_assoc($sql)) {
    $alpha[$records['alpha']] += 1;
    ${$records['alpha']}[$records['id_clients']] = array(
        $records['first_name'],       //item[0]
        $records['subMiddleName'],    //item[1]
        $records['last_name'],        //item[2]
        $records['address'],          //item[3]
        $records['primary_number'],   //item[4]
        $records['secondary_number'], //item[5]
        $records['home_number'],      //item[6]
        $records['office_number'],    //item[7]
        $records['cell_number'],      //item[8]
        $records['fax_number'],       //item[9]
        $records['ext_number'],       //item[10]
        $records['other_number'],     //item[11]
        $records['comments'],         //item[12]
        $records['type']              //item[13]
    );
}

如果我使用 while 循环,那么我的页面将锁定并抛出带有 60 秒超时消息的胎儿错误。如果我使用 foreach 循环,那么它似乎会通过,但现在我的结果是......奇怪......

这些结果来自使用 foreach 循环和 PDO 查询

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
B

B B B


B


primary: (B)


secondary: (B)


B
E

E E E


E


primary: (E)


secondary: (E)


E
O

O O O


O


primary: (O)


secondary: (O)


O

这些是我应该得到的结果。这些结果来自使用 while 循环和 mysql 查询

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
O

Eli O Orellana


5542 Crossover Prk


primary: 000.000.0000 (cell)


secondary: 000.000.0000 (home)


Buyer

Sandra Y Orellana


123 Maple Ln


primary: 000.000.0000 (cell)


secondary: 000.000.0000 (home)


Seller

Josue Orellana




primary: (Home)


secondary: (Cell)


Buyer

编辑:其余代码

// Create Alpha link Listing
foreach(range('A','Z') as $i) {
    echo (array_key_exists ("$i", $alpha)) ? '<a href="#'.$i.'" title="'.$alpha["$i"].' results">'.$i.'</a>' : "$i";
    echo ($i != 'Z') ? ' | ':'';
}

// Create Data Listing
        foreach(range('A','Z') as $i) {
            if (array_key_exists ("$i", $alpha)) {
                echo '<div class="alpha"><span class="expand-alpha-up"></span><a href="#" name="'.$i.'">'.$i.'</a></div><div class="show">';
                foreach ($$i as $key=>$item)
                    echo '
                    <table cellpadding="0" cellspacing="0" width="99%">
                        <tr>
                            <td class="td-name"><p><a href="#" id="'.$key.'">'.Truncate($item[0].' '.$item[1].' '.$item[2], 17, true).'</a></p></td>
                            <td class="td-address"><p>'.Truncate($item[3], 20, true).'</p></td>
                            <td class="td-contact"><p>primary: '.($item[4] == 'home' ? $item[6] : ($item[4] == 'office' ? $item[7] : ($item[4] == 'cell' ? $item[8] : ($item[4] == 'fax' ? $item[9] : ($item[4] == 'other' ? $item[11] : ''))))).'<i> ('.$item[4].')</i></p></td>
                            <td class="td-contact"><p>secondary: '.($item[5] == 'home' ? $item[6] : ($item[5] == 'office' ? $item[7] : ($item[5] == 'cell' ? $item[8] : ($item[5] == 'fax' ? $item[9] : ($item[5] == 'other' ? $item[11] : ''))))).'<i> ('.$item[5].')</i></p></td>
                            <td class="td-type"><p><strong>'.$item[13].'</strong></p></td>
                        </tr>
                    </table>';
            }
            echo '</div>';
        }
    }
}

也是我的 fetch() 方法

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(PDO::FETCH_ASSOC);
            return $result;
        }catch(PDOException $e){
            echo $e->getMessage();
        }
        return false;
    }

This is my code

public function alpha()
{
    $alpha = NULL;
    $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`";

//$query = mysql_query ($sql) or die (mysql_error());
//echo "test";
foreach($this->db->fetch_row_assoc($sql) as $records) {
//while ($records = $this->db->fetch_row_assoc($sql)) {
    $alpha[$records['alpha']] += 1;
    ${$records['alpha']}[$records['id_clients']] = array(
        $records['first_name'],       //item[0]
        $records['subMiddleName'],    //item[1]
        $records['last_name'],        //item[2]
        $records['address'],          //item[3]
        $records['primary_number'],   //item[4]
        $records['secondary_number'], //item[5]
        $records['home_number'],      //item[6]
        $records['office_number'],    //item[7]
        $records['cell_number'],      //item[8]
        $records['fax_number'],       //item[9]
        $records['ext_number'],       //item[10]
        $records['other_number'],     //item[11]
        $records['comments'],         //item[12]
        $records['type']              //item[13]
    );
}

If I use the while loop then my page locks up and throws out a Fetal Error with a 60 sec timeout message. If I use the foreach loop then It seems to go thru but now my results are ...weird...

These results are from using the foreach loop with a PDO query

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
B

B B B


B


primary: (B)


secondary: (B)


B
E

E E E


E


primary: (E)


secondary: (E)


E
O

O O O


O


primary: (O)


secondary: (O)


O

These are the results I should be getting. These results are from using the while loop with mysql queries

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
O

Eli O Orellana


5542 Crossover Prk


primary: 000.000.0000 (cell)


secondary: 000.000.0000 (home)


Buyer

Sandra Y Orellana


123 Maple Ln


primary: 000.000.0000 (cell)


secondary: 000.000.0000 (home)


Seller

Josue Orellana




primary: (Home)


secondary: (Cell)


Buyer

EDIT: Rest of code

// Create Alpha link Listing
foreach(range('A','Z') as $i) {
    echo (array_key_exists ("$i", $alpha)) ? '<a href="#'.$i.'" title="'.$alpha["$i"].' results">'.$i.'</a>' : "$i";
    echo ($i != 'Z') ? ' | ':'';
}

// Create Data Listing
        foreach(range('A','Z') as $i) {
            if (array_key_exists ("$i", $alpha)) {
                echo '<div class="alpha"><span class="expand-alpha-up"></span><a href="#" name="'.$i.'">'.$i.'</a></div><div class="show">';
                foreach ($i as $key=>$item)
                    echo '
                    <table cellpadding="0" cellspacing="0" width="99%">
                        <tr>
                            <td class="td-name"><p><a href="#" id="'.$key.'">'.Truncate($item[0].' '.$item[1].' '.$item[2], 17, true).'</a></p></td>
                            <td class="td-address"><p>'.Truncate($item[3], 20, true).'</p></td>
                            <td class="td-contact"><p>primary: '.($item[4] == 'home' ? $item[6] : ($item[4] == 'office' ? $item[7] : ($item[4] == 'cell' ? $item[8] : ($item[4] == 'fax' ? $item[9] : ($item[4] == 'other' ? $item[11] : ''))))).'<i> ('.$item[4].')</i></p></td>
                            <td class="td-contact"><p>secondary: '.($item[5] == 'home' ? $item[6] : ($item[5] == 'office' ? $item[7] : ($item[5] == 'cell' ? $item[8] : ($item[5] == 'fax' ? $item[9] : ($item[5] == 'other' ? $item[11] : ''))))).'<i> ('.$item[5].')</i></p></td>
                            <td class="td-type"><p><strong>'.$item[13].'</strong></p></td>
                        </tr>
                    </table>';
            }
            echo '</div>';
        }
    }
}

Also my fetch() method

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(PDO::FETCH_ASSOC);
            return $result;
        }catch(PDOException $e){
            echo $e->getMessage();
        }
        return false;
    }

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

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

发布评论

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

评论(1

电影里的梦 2024-11-03 00:50:26

尝试使用此 PDO 帮助程序类: Github 上的 PDO 帮助程序类

将查询字符串传递给 getQuery( )

require_once("Database.php");
$db = new DB;

$data = $db->getQuery($sql);

foreach($data as $d)
{
    $d['first_name']; // etc
}

看看是否有帮助。

Try using this PDO helper class: PDO Helpers Class on Github

Pass the query string to getQuery()

require_once("Database.php");
$db = new DB;

$data = $db->getQuery($sql);

foreach($data as $d)
{
    $d['first_name']; // etc
}

see if that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文