简体   繁体   English

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

[英]I'm getting weird results with this block of code. foreach loop not doing the job, and while loop times out. Using PDO for queries

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. 如果我使用while循环,那么我的页面将锁定并抛出60秒超时消息,并抛出致命错误。 If I use the foreach loop then It seems to go thru but now my results are ...weird... 如果我使用foreach循环,那似乎可以通过,但现在我的结果很奇怪...

These results are from using the foreach loop with a PDO query 这些结果来自将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

These are the results I should be getting. 这些是我应该得到的结果。 These results are from using the while loop with mysql queries 这些结果来自对MySQL查询使用while循环

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 还有我的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;
    }

Try using this PDO helper class: PDO Helpers Class on Github 尝试使用以下PDO帮助器类: Github上的PDO帮助器类

Pass the query string to getQuery() 将查询字符串传递给getQuery()

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

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

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

see if that helps. 看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM