Mysqli查询无法在服务器中运行并且本地运行正常

时间:2017-01-20 17:28:58

标签: php mysql mysqli

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

    <?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是从另一个文件中调用的。

2 个答案:

答案 0 :(得分:1)

这可能很明显,但如果您还没有这样做,请更改您的服务器名称。如果它在localhost上工作,那是因为你的服务器名是localhost。在您的网站上转到您的控制面板并找到mysql区域,它将为您提供您应该使用的服务器名称,但它可能被称为主机名称。将其复制到您拥有localhost的位置。此外,数据库名称可能会有所不同,但它将列在您的Web服务器控制面板上。

答案 1 :(得分:1)

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

'SELECT * FROM student WHERE CHEST = 0';

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

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