使用mysql fetch行发出检索值

时间:2013-12-17 20:04:34

标签: php mysql

我在从mysql表中返回特定值时遇到问题。代码似乎失败了:

$role = $row[7];

线。我用echo测试了这个。

我的功能如下:

function fetchRole(){

$query = "SELECT * FROM membersTable WHERE username='$user'";
$result = queryMysql($query);

$row = mysql_fetch_row($result);

$role = $row[7];

switch ($role)
{
    case "HeadCoach":   $role = 'Head Coach'; break;
    case "Coach":       $role = 'Team Coach'; break;
    case "Science":     $role = 'Sport Scientist'; break;
    case "Strength":    $role = 'Strength & Conditioning'; break;
    case "Physio":      $role = 'Physiotherapist'; break;
    case "GK":          $role = 'Goalkeeper'; break;
    case "FB":          $role = 'Full-Back'; break;
    case "LB":          $role = 'Left-Back'; break;
    case "CB":          $role = 'Centre-Back'; break;
    case "RB":          $role = 'Right-Back'; break;
    case "LM":          $role = 'Left-Midfielder'; break;
    case "CM":          $role = 'Central-Midfielder'; break;
    case "RM":          $role = 'Right-Midfielder'; break;
    case "AML":         $role = 'Attacking-Midfielder (Left)'; break;
    case "AMC":         $role = 'Attacking-Midfielder (Central)'; break;
    case "AMR":         $role = 'Attacking-Midfielder (Right)'; break;
    case "WF":          $role = 'Wide Forward'; break;
    case "CF":          $role = 'Centre Forward'; break;                
    default:            $role = 'Unknown';
}   
return $role;
}

membersTable架构如下所示:

id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(16),
lastName VARCHAR(16),
dob DATE,
height CHAR(3),
weight CHAR(3),
type CHAR(1),
position VARCHAR(16),
teamName VARCHAR(32),
status VARCHAR(16),
username VARCHAR(16),
pass VARCHAR(16),
INDEX(firstName(6)),
INDEX(lastName(6))

我查询mysql是一个我知道可以在我网站上的其他区域工作的函数。

目前正在回显$ row打印出RESOURCE#19。

此前一个功能有效:

function generateStafflist()

{

echo "<h3>Team Staff</h3>";

$positionArray = array('HeadCoach','Coach','Science','Strength','Physio');

$query = "SELECT * FROM membersTable WHERE type='S'";
$result = queryMysql($query);

$rows = mysql_num_rows($result);

$count = count($positionArray);

for($i = 0; $i < $count; ++$i)
{
    $result = queryMysql($query);

    for($j = 0; $j < $rows; ++$j)
    {
        $row = mysql_fetch_row($result);

        $position = $row[7];
        $currentPosition = $positionArray[$i];

        if($position == $currentPosition)
        {

            echo    "<div class='snapshot'>";

            if(file_exists($row[9] . ".jpg"))
            {
                echo "<img src='".$row[9].".jpg' height='210' width='180' />";
            }
            else
            {
                echo "<img src='imgs/profiles/noimage.png' height='210' width='180' />";
            }
            echo    $row[1] . " " . $row[2] . "<br />";

            switch($row[7])
            {
                case "HeadCoach":   $role = 'Head Coach'; break;
                case "Coach":       $role = 'Team Coach'; break;
                case "Science":     $role = 'Sport Scientist'; break;
                case "Strength":    $role = 'Strength & Conditioning'; break;
                case "Physio":      $role = 'Physiotherapist'; break;
                default:            $role = '';
            }

            echo    '[' . $role . ']<br />';
            echo    '</div>';
        }
    }
}

}

3 个答案:

答案 0 :(得分:0)

看起来您的查询失败的原因是因为您使用的是$user变量,而没有将其初始化为任何内容:

$query = "SELECT * FROM membersTable WHERE username='$user'";

上述基本上将对此进行评估:

$query = "SELECT * FROM membersTable WHERE username=''";

这很可能不是你想要的,它可能不匹配你表中的任何记录,所以$result自然是空的。

也就是说,您直接查询数据库的方法会让您面临严重的SQL注入漏洞。您应该尽可能使用参数化查询,如果您可以使用任何框架,那么您可以使用数据访问抽象层。

此外,使用诸如SELECT * FROM等全包式查询是非常糟糕的形式。如果数据库架构发生变化,您将打开代码以应对各种潜在的错误。所需要的只是重新排序表中的字段以弄乱您的代码。只查询您感兴趣的特定字段会好得多:

$query = "SELECT position FROM membersTable WHERE username='$user'";

答案 1 :(得分:0)

更新: 使用mysql_fetch_assoc,而不是mysql_fetch_row,因为它可能是错误的......

我可能错了,但我认为我是对的。

提示:使用PDO会更好。

答案 2 :(得分:0)

首先,正如这里说的十亿次,你必须使用mysqli ......

其次,你必须循环你的mysql_fetch_row(),如果它返回多行;

$rowarr = array();
while ($row = mysql_fetch_row($result)) {
  array_push($rowarr, $row);
}

$role = $rowarr[x][7];

P.S。你总是可以var_dump($row)看看结构是什么样的。