【发布时间】:2014-03-24 19:16:24
【问题描述】:
这是一个学校项目,我不知道为什么,但它只返回两列。我查询的数据库图片是:
.
自从截屏以来,我将 NULL 字段更改为 1,认为它不会打印 NULL。
这个项目的网页在my project。
在建表步骤之前,我做了测试,看看数组返回了什么,这正是我所期望的。先前数据库中的 NULL 值存在空白。我在想它可能是阻止它返回其余数组的数字,但是我正在使用的命令 IIRC(如果我回忆)仅将字符串返回到数组。我可能不正确,但这是我的理解。
记住:链接的数据库图像中的 NULL 现在是 1(int 类型)。
我没有使用框架,我正在做所有内联编码(为了经验)。这是第一次使用 PHP。我查看了许多其他示例,但我似乎无法根据我自己的问题调整他们的解决方案。任何帮助将不胜感激!
编辑:我忘记打印我的测试中的数组输出。它是 (与上面几乎相同的 url,只需删除 main.php 并添加 test.php),我忘了提到 varchar 数据类型的 NULL 字段被替换为 NA 而不是我的 1编辑图像以反映这一点。
<?php
require_once(file is there...) // removed from example for security reasons.
// Test message
echo '<p>Connect please...</p>';
try {
// Connection from PHP to MSSQL via PDO
// DataBase Host
$dbh = new PDO($dsn, $user, $password);
//Sent/Send To Host and preparing the query to send.
$sth = $dbh->prepare("SELECT * FROM Backbar");
//Send that query!
$result = $sth->execute();
// Fetch all of the remaining rows in the result set
$row = $sth->fetchAll();
// Used to move through array.
$subarraypos = 0;
// Start the table format extravaganza!
print('<table border="1px">');
print('<tr><th>ProductCompany</th>
<th>ProductName</th>
<th>Barcode#</th>
<th>Color</th>
<th>Perms</th>
<th>Cost</th>
<th>Volume</th>
<th>Size</th>
<th>Shelf Location</th>
<th>Shelf Cap</th>
<th>Quantity On Hand</th>
<th>Storage Location</th>
<th>Quantity In Storage</th>
<th>Vendor</th>
<th>Vendor Region</th>
<th>Vendor Mileage</th>');
foreach($row as $value) {
if($subarraypos === 0) {
print('<tr><td>'.$value[$subarraypos].'</td>');
$subarraypos++;
}
// 16 will reset the number of columns from the
// database so each entry is correctly shown.
// Else of first if(...) (Elif)
if($subarraypos <=15) {
print('<td>'.$value[$subarraypos].'</td>');
$subarraypos++;
}
// Else of second if(...) (Else)
$subarraypos = 0;
print('</tr>');
}
print('</table>');
print('<p>'.$row.'</p>');
print('<p>'.$value.'</p>');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
echo "<p>You should see a table, unless I broke it.</p>";
unset($dbh);
?>
【问题讨论】:
-
您正在调用 foreach 循环来循环遍历行,而不是遍历每一行的内容。
标签: php arrays sql-server html-table