我用这段代码得到了奇怪的结果。 foreach 循环没有完成工作,而 while 循环超时。使用 PDO 进行查询
这是我的代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用此 PDO 帮助程序类: Github 上的 PDO 帮助程序类
将查询字符串传递给 getQuery( )
看看是否有帮助。
Try using this PDO helper class: PDO Helpers Class on Github
Pass the query string to getQuery()
see if that helps.