我不明白这里发生了什么。
这是我的代码:

    <?php
function chestnum(){
  $servername = "localhost";
  $username = "athletics";
  $password = "";
  $dbname = "athletics";
  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  // Check connection
  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  }
  //selecting all chest number unallotted students
  $query = 'SELECT * FROM student WHERE CHEST = 0';
  $result = mysqli_query($conn, $query);
  //selecting all departments' max chest number
  //$query = 'SELECT MAX(CHEST) FROM student GROUP BY batch';
  //$max = mysqli_query($conn, $query);
  while($row = mysqli_fetch_assoc($result)){
    $batch = $row['batch'];
    $roll =  $row['roll'];
    $q = "SELECT MAX(`CHEST`) FROM `STUDENT` WHERE `batch`='$batch'";
    //$q = "SELECT MAX(CHEST) WHERE batch = $batch";
    $allmax = mysqli_query($conn, $q);
    while($rowm = mysqli_fetch_assoc($allmax))
     {
         echo $max=$rowm['MAX(`CHEST`)'];
     }
     //echo $cse = $bcr;echo $csa = $bcr;echo $me = $bcr; echo $ece = $bcr;echo $eee = $bcr;
/*
  CHEST NUMBER ALLOCATION INFO
  //100-250mech, 251-400ec, 401-550eee, 551-700cse, 701-850csa
*/
    //Checking for the department and setting chest number
    if($batch==1 && $max==0){
      $q = "UPDATE student SET CHEST = 100 WHERE roll LIKE '$roll'";
    } else if($batch==1 && $max == 250) {
      $q = "UPDATE student SET CHEST = 900 WHERE roll LIKE '$roll'";
    } else if($batch==1){
      $max++;
      $q = "UPDATE student SET CHEST = $max WHERE roll LIKE '$roll'";
    }
    if($batch==2 && $max==0){
      $q = "UPDATE student SET CHEST = 251 WHERE roll LIKE '$roll'";
    } else if($batch==2 && $max == 400) {
      $q = "UPDATE student SET CHEST = 1000 WHERE roll LIKE '$roll'";
    } else if($batch==2) {
      $max++;
      $q = "UPDATE student SET CHEST = $max WHERE roll LIKE '$roll'";
    }
    if($batch==3 && $max==0){
      $q = "UPDATE student SET CHEST = 401 WHERE roll LIKE '$roll'";
    }  else if($batch==3 && $max == 550) {
      $q = "UPDATE student SET CHEST = 1100 WHERE roll LIKE '$roll'";
    } else if($batch==3) {
      $max++;
      $q = "UPDATE student SET CHEST = $max WHERE roll LIKE '$roll'";
    }
    if($batch==4 && $max==0){
      $q = "UPDATE student SET CHEST = 551 WHERE roll LIKE '$roll'";
    } else if($batch==4 && $max == 700) {
      $q = "UPDATE student SET CHEST = 1200 WHERE roll LIKE '$roll'";
    } else if($batch==4) {
      $max++;
      $q = "UPDATE student SET CHEST = $max WHERE roll LIKE '$roll'";
    }
    if($batch==5 && $max==0){
      $q = "UPDATE student SET CHEST = 701 WHERE roll LIKE '$roll'";
    } else if($batch==5 && $max == 850) {
      $q = "UPDATE student SET CHEST = 1300 WHERE roll LIKE '$roll'";
    } else if($batch==5) {
      $max++;
      $q = "UPDATE student SET CHEST = $max WHERE roll LIKE '$roll'";
    }
    //assigning the chest number
    mysqli_query($conn, $q);
  }
}
?>

代码在本地计算机中测试时运行良好,但在服务器中执行时不显示任何内容。执行时没有错误。函数chestnum是从另一个文件调用的。

最佳答案

我以前遇到过这个问题。这里的问题是表名。表名区分大小写。

'SELECT * FROM student WHERE CHEST = 0';

"SELECT MAX(`CHEST`) FROM `STUDENT` WHERE `batch`='$batch'";

将表名更改为STUDENT(并确保在mysql服务器中相同)。

关于php - Mysqli查询在服务器中不起作用并且在本地运行良好,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41769267/

10-11 10:46