绝对的新手在这里。我有一个简单的问题:
$result = mysql_query("SELECT * FROM evalucion WHERE username = '$empleado'") or die(mysql_error());
$row = mysql_fetch_array( $result );
if ( $row['relacion'] == 'Team') {
while ($row = mysql_fetch_array( $result)) {
...
}
}
事情是,当我这样做时,数组显示的结果少了一个。还有另一种方法吗?
答案 0 :(得分:1)
使用if inside loop
$result = mysql_query("SELECT * FROM evalucion WHERE username = '$empleado'") or die(mysql_error());
while($row = mysql_fetch_array( $result ) ) { if ( $row['relacion'] == 'Team') {
答案 1 :(得分:0)
这种情况正在发生,因为if ( $row['relacion'] == 'Team')
只检查了一个值,当此if
条件失败时,控件永远不会返回while
来提取来自{{1}的剩余数据查询
要过滤掉所有值,首先,您需要遍历所有值,然后检查SELECT
条件。
要实现这一点,只需交换现有代码,如下所示:
if
答案 2 :(得分:0)
如果您想保持相同的结构,请使用do while
而不是简单的while
(如果您有充分理由这样做的话)
$row = mysql_fetch_array( $result );
if ( $row['relacion'] == 'Team') {
do {
// Your code here
}
while ($row = mysql_fetch_array( $result ));
}
答案 3 :(得分:0)
你应该改变你的sql以避免一起出现问题:
"SELECT * FROM evalucion WHERE username = '$empleado' AND relacion = 'Team'"
答案 4 :(得分:0)
$result = mysql_query("SELECT * FROM evalucion WHERE username = '$empleado'") or die(mysql_error());
$new_arr = array();
while($row = mysql_fetch_array( $result ) )
{
if ( $row['relacion'] == 'Team')
{
// do whatever you want;
}
}
答案 5 :(得分:0)
这取决于您尝试做什么,但您可以使用mysql_data_seek()
来重置数组指针。
http://php.net/manual/en/function.mysql-data-seek.php
$result = mysql_query("SELECT * FROM evalucion WHERE username = '$empleado'") or die(mysql_error());
$row = mysql_fetch_array($result);
// Internal pointer has now been moved on +1 when you
// called mysql_fetch_array() above, so your next loop through
// the $result will start from the second row.
// So reset it to the beginning before the next while loop.
mysql_data_seek($result, 1);
if ($row['relacion'] == 'Team') {
while($row = mysql_fetch_array($result)) {
...