PHP / MySQL挑剔数据选择

时间:2015-09-19 00:37:03

标签: php mysql

我遇到了一些问题。我试图从行中的值相同的列中提取数据。我使用SELECT * FROM teams where number=$number 用户指定$number的位置。它适用于某些行,但有些不行。这非常奇怪。

http://pastebin.com/4CK3mmpd

<link rel='icon' href='/image/database_accepted.png' />
<body style="background-color:black">
<?php
   include 'includes/connect.php';
    //Include the login varibles

    //Setup a query for all teams into a varible

    $number_select = $_GET['team'];

    $query = "SELECT * FROM teams WHERE number='$number_select'";
    $result = mysql_query($query);
    $test = mysql_fetch_array($result);
     if($test==null){
        echo "<img src='/image/no_data_found_paramaters.png'>";
    }else{
        }
?>

<style>
table, th, td {
    border: 1px solid black;
}
</style>
<?php
echo "<table bgcolor='#00FF00'>";
echo "<tr>";
echo "<th>Team Name</th>";
echo "<th>Team Number</th>";
echo "</tr>";
echo "<font color='red'>";
    while($team = mysql_fetch_array($result)) {
echo  "<tr>";
echo    "<td>" . $team['Name'] . "</td>";
echo    "<td>" . $team['Number'] . "</td>";
echo   "</tr>";


    }
?>

编辑:请求的操作粘贴:

CREATE TABLE `teams` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(25) NOT NULL,
  `Number` int(4) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8

1 个答案:

答案 0 :(得分:0)

转换到像mysqli或pdo这样的安全库,并以适当的方式使用它们。看到这个广受欢迎的link

这不是你的问题。由于拨打fetch两次(第一次在while之外),您从未获得第一行。

因此,如果您没有重复数字,那么唯一要获取的行已经被提取,并且从未进入while循环。在下面的模式中,数字9只会在html表中显示2行而不是3行。你明白了。

试试这个:

<link rel='icon' href='/image/database_accepted.png' />
<body style="background-color:black">
<?php
   include 'includes/connect.php';
    //Include the login varibles

    //Setup a query for all teams into a varible

    $number_select = $_GET['team'];

    $query = "SELECT * FROM teams WHERE number='$number_select'";
    $result = mysql_query($query);
    //$test = mysql_fetch_array($result);
    // if($test==null){
    //    echo "<img src='/image/no_data_found_paramaters.png'>";
    //}else{
    //    }
?>

<style>
table, th, td {
    border: 1px solid black;
}
</style>
<?php
echo "<table bgcolor='#00FF00'>";
echo "<tr>";
echo "<th>Team Name</th>";
echo "<th>Team Number</th>";
echo "</tr>";
echo "<font color='red'>";
    while($team = mysql_fetch_array($result)) {
echo  "<tr>";
echo    "<td>" . $team['Name'] . "</td>";
echo    "<td>" . $team['Number'] . "</td>";
echo   "</tr>";
    }
?>

如果有人使用此

模仿您的数据,很容易看到

模式

CREATE TABLE `teams` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(25) NOT NULL,
  `Number` int(4) NOT NULL,
  PRIMARY KEY (`ID`)
)

insert teams (name,number) values ('aaaa',9);
insert teams (name,number) values ('aaaa',9);
insert teams (name,number) values ('zzzz',9);

insert teams (name,number) values ('blah',10);