PHP和MySQL:是什么导致查询排除表中的第一条记录:例如,我有一个脚本,如下所示:
$query = "SELECT * FROM cars WHERE car_name = 'BMW'";
$results = mysql_query($query);
echo "<table border='1'>";
echo "<tr>";
echo "<th>Vehicle Name:<th>";
echo "</tr>";
while($row = mysql_fetch_array($result)){
$name = $row['car_name'];
echo "<tr>";
echo "<td>$name<td>";
echo "</tr>";
}
echo "</table>";所有行都会返回,除了第一个one.Please help a brother out first。
发布于 2015-02-04 21:25:48
编辑
If two or more columns of the result have the same field names, the last
column will take precedence. To access the other column(s) of the same name,
you must use the numeric index of the column or make an alias for the
column. For aliased columns, you cannot access the contents with the
original column name您正在使用mysql_fetch_array,这就是文档中所说的。我从不使用mysql*函数,因此我不会很快得出这种类型的结论。使用mysql_fetch_assoc($results),我有99%的把握它会解决你的问题。为什么这将是不同的是在上面的段落与文档。我假设您的第一行与下面的行中至少有一行相同。这意味着你很可能会错过比第一个更多的东西。可能是也可能不是。
结束编辑
在你的代码中添加4件事。
echo "<tr>";
echo "<th>ID</th>"; // THIS
echo "<th>Vehicle Name:</th>"; // Add closing tags........
echo "</tr>";
echo mysql_num_rows($results); // THIS (compare this to your MYSQL output row count)
while($row = mysql_fetch_array($results)){ **THIS... you have $results set with query, but $result here*** make sure both are $results OR $result
$id = $row['YOUR_AI_ID']; // THIS
$name = $row['car_name'];
echo "<tr>";
echo "<td>$id</td>"; // THIS
echo "<td>$name</td>"; // Add closing tags.......
echo "</tr>";
}去吧,去做吧。带着结果回来。
发布于 2015-02-04 21:34:13
不是答案,但评论太长了:
让我们在您的cars表上达到顶峰。
什么是
$qs = array(
array('total #rows', 'SELECT Count(*) FROM cars'),
array('#BMW', "SELECT Count(*) FROM cars WHERE car_name='BMW'"),
array('#LIKE BMW', "SELECT Count(*) FROM cars WHERE car_name LIKE '%BMW%'"),
array('#car_names', "SELECT Count(*) FROM (SELECT distinct car_name as foo FROM cars) as bar")
);
foreach( $qs as $query ) {
echo $query[0], "<br />\r\n";
$result = mysql_query($query[1]) or die(mysql_error());
while ( false!==($row=mysql_fetch_row($result)) ) {
echo ' ', $row[0], "\r\n";
}
}如果放置在您的脚本中而不是发布的代码中,是否进行打印?
输出应该是这样的
total #rows<br />
6
#BMW<br />
2
#LIKE BMW<br />
3
#car_names<br />
4顺便说一句: mysql_*扩展已被弃用,
https://stackoverflow.com/questions/28321978
复制相似问题