Reputation:
I don't understand what is happening here. This is my code:
<?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);
}
}
?>
The code runs fine when tested in my local machine but it is showing nothing when executed in the server. There is no error when executing. The function chestnum is called from an other file.
Upvotes: 1
Views: 1474
Reputation: 2516
I have faced this issue before. The table name is the problem here. Table name is case-sensitive.
'SELECT * FROM student WHERE CHEST = 0';
"SELECT MAX(`CHEST`) FROM `STUDENT` WHERE `batch`='$batch'";
change the table name to STUDENT (and make sure in the mysql server it is the same).
Upvotes: 0
Reputation: 63
This may be obvious, but if you haven't done so, change your servername. if it works on the localhost, that's because your servername is localhost. On your website go to your control panel and find the mysql area, it will give you the server name you should use, it might be called Host Name though. Copy that to where you have localhost. Also the database name will likely be different, but it will be listed on your web servers control panel.
Upvotes: 1