很抱歉,如果标题的措辞有点奇怪和尴尬。
我有一个通过 AJAX/PHP/MySQL 创建表的系统,但由于某种原因我遇到了一些问题。
首先,这是在表格中回显结果的 PHP
if(mysql_num_rows($sql)>0) {
$quote=$quote."<h3>Hard Enamel</h3>";
while($row = mysql_fetch_array($sql)){
$quote=$quote."<p>Size: ".$row["type"]."<br />Extras: ".$HEextras."</p>
<table border='1' width='300' cellpadding='3'>
<tr>
<th width='100'>Quantity</th>
<th width='100'>Unit Cost</th>
<th width='100'>Setup</th>
</tr>";
if($row["100"]) {
$quote=$quote."<tr><td>100</td><td>".cost($row["100"])."</td><td>".setup($row["setup"])."</td></tr>";
}
if($row["250"]) {
$quote=$quote."<tr><td>250</td><td>".cost($row["250"])."</td><td>".setup($row["setup"])."</td></tr>";
}
if($row["500"]) {
$quote=$quote."<tr><td>500</td><td>".cost($row["500"])."</td><td>".setup($row["setup"])."</td></tr>";
}
if($row["1000"]) {
$quote=$quote."<tr><td>1,000</td><td>".cost($row["1000"])."</td><td>".setup($row["setup"])."</td></tr>";
}
if($row["2500"]) {
$quote=$quote."<tr><td>2,500</td><td>".cost($row["2500"])."</td><td>".setup($row["setup"])."</td></tr>";
}
if($row["5000"]) {
$quote=$quote."<tr><td>5,000</td><td>".cost($row["5000"])."</td><td>".setup(0)."</td></tr>";
}
if($row["10000"]) {
$quote=$quote."<tr><td>10,000</td><td>".cost($row["10000"])."</td><td>".setup(0)."</td></tr>";
}
$quote=$quote."<br />";
}
echo $quote;
}
在测试时,我让 SQL 返回 2 行数据,其中包含 type
、100
、250
和 setup
列>,type
列值为 12mm 和 16mm。这些返回正常,设置值也返回正常,但列 100
和 250
的值返回列标题。也尝试过不使用 cost() 函数,但没有改变任何东西。
而且,它读取的顺序是错误的,我不知道为什么!返回结果如下。
Hard Enamel
Size: 12mm
Extras: extrastring
Size: 16mm
Extras: extrastring
Quantity Unit Cost Setup
100 100 40
250 250 40
Quantity Unit Cost Setup
100 100 40
250 250 40
感谢任何帮助,干杯。
最佳答案
如果我不得不大胆猜测,它可能与使用 mysql_fetch_array()
以及数字列名有关。默认情况下,mysql_fetch_array()
函数将返回一个具有位置索引和标签索引的数组。有些东西可能会变得困惑。
我会尝试:
- 使用
mysql_fetch_assoc()
代替mysql_fetch_array()
- 将数据库中的列名称更改为非数字(无论如何,这是一个好主意)
- 确保您的查询正确转义列名(如果它们仍然是数字)
如果这些都没有任何效果,那么查看数据库中包含的确切数据以及用于获取它的查询会很有帮助,因为这些也可能是潜在的问题。
关于PHP/MySQL 以错误的顺序回显并且还回显列标题而不是该列的行值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19454716/