mysqli获取行更短

时间:2014-05-19 15:00:18

标签: php mysqli

所以我正在为朋友制作剧本。使用mysqli。 我想知道是否有一种方法可以命令脚本一次性获取它而不是全部采用它。有没有办法像在Mysql中一样获取它?

代码:

 if(isset($_GET['event']) && isset($_GET['unique_id']) && isset($_GET['local_id'])&& isset($_GET['username'])) {
      $unique_id=$_GET["unique_id"];
         $local_id=$_GET["local_id"];
         $username=$_GET["username"];

      $db_handle = mysqli_connect("127.0.0.1", "root", "pass", "oasisdata") or die("|-1");
      if($db_handle)
        if($_GET['event'] == 1)
        {
          $sql_result = mysqli_query($db_handle, "SELECT Bank FROM playerdata WHERE Unique_Id = '$unique_id'") or die("Error: ".mysqli_error($db_handle));
          $result = mysqli_fetch_row($sql_result);

          if(!isSet($result)) {
             mysqli_query($db_handle, "INSERT INTO playerdata (Unique_Id,Username,Gold, Bank, Troop) VALUES($unique_id, '$username', 5000, 5000, 4)");
            echo "1|$unique_id|$local_id|5000|5000|4|0|86|0|0|0|0|0|0|0|0|0|0|0|0|";
        }else{
          $fetch = mysqli_query($db_handle, "SELECT Gold FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row = mysqli_fetch_array($fetch);
          $fetch1 = mysqli_query($db_handle, "SELECT Bank FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row1 = mysqli_fetch_array($fetch1);
          $fetch2 = mysqli_query($db_handle, "SELECT Troop FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row2 = mysqli_fetch_array($fetch2);
          $fetch3 = mysqli_query($db_handle, "SELECT Head FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row3 = mysqli_fetch_array($fetch3);
          $fetch4 = mysqli_query($db_handle, "SELECT Body FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row4 = mysqli_fetch_array($fetch4);
          $fetch5 = mysqli_query($db_handle, "SELECT Gloves FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row5 = mysqli_fetch_array($fetch5);
          $fetch6 = mysqli_query($db_handle, "SELECT Foot FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row6 = mysqli_fetch_array($fetch6);
          $fetch7 = mysqli_query($db_handle, "SELECT CombatLog FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row7 = mysqli_fetch_array($fetch7);
          $fetch8 = mysqli_query($db_handle, "SELECT Item0 FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row8 = mysqli_fetch_array($fetch8);
          $fetch9 = mysqli_query($db_handle, "SELECT Item1 FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row9 = mysqli_fetch_array($fetch9);
          $fetch10 = mysqli_query($db_handle, "SELECT Item2 FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row10 = mysqli_fetch_array($fetch10);
          $fetch11 = mysqli_query($db_handle, "SELECT Item3 FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row11 = mysqli_fetch_array($fetch11);
          $fetch12 = mysqli_query($db_handle, "SELECT CoordinateX FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row12 = mysqli_fetch_array($fetch12);
          $fetch13= mysqli_query($db_handle, "SELECT CoordinateY FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row13 = mysqli_fetch_array($fetch13);
          $fetch14 = mysqli_query($db_handle, "SELECT CoordinateZ FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row14 = mysqli_fetch_array($fetch14);
          $fetch15 = mysqli_query($db_handle, "SELECT Horse FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row15 = mysqli_fetch_array($fetch15);
          $fetch16 = mysqli_query($db_handle, "SELECT Hp FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row16 = mysqli_fetch_array($fetch16);
          $fetch17 = mysqli_query($db_handle, "SELECT New FROM playerdata WHERE Unique_Id = '$unique_id'" ); 
          $row17 = mysqli_fetch_array($fetch16);
          echo "1|$unique_id|$local_id|$row[Gold]|$row1[Bank]|$row2[Troop]|$row3[Head]|$row4[Body]|$row5[Gloves]|$row6[Foot]|$row7[CombatLog]|$row8[Item0]|$row9[Item1]|$row10[Item2]|$row11[Item3]|$row12[CoordinateX]|$row13[CoordinateY]|$row14[CoordinateZ]|$row15[Horse]|$row16[Hp]|$row17[New]|"; 
         }

如果有任何错误。请说出来。 感谢。

1 个答案:

答案 0 :(得分:2)

这是基本的SQL。只需在一个查询中选择所有这些列,只需用逗号SELECT col1, col2, col3 FROM table分隔。

$query = "SELECT 
    Gold, Bank, Troop, Head, Body, Gloves, Foot, 
    CombatLog, Item0, Item1, Item2, Item3, CoordinateX, 
    CoordinateY, CoordinateZ ,Horse, Hp, New 
FROM playerdata 
WHERE Unique_Id = '$unique_id'"
$fetch = mysqli_query($db_handle, $query); 
$row = mysqli_fetch_assoc($fetch);
echo $row['Gold'];// outputs value of GOLD column

仅供参考,您对SQL injections持开放态度。您需要清理并转义用户输入或使用预准备语句。