I have been writing the script of a cron job in PHP that is supposed to connect to a text file, get the participantID that is written to it using Java separately, and return details about that participant in a separate text file. The details about that participant are supposed to be their Points, Quantity and Rank out of other participants(which is got using the points that the respective participants have got).
This is the code that I have written so far:
<?php
//connect to mysql database procedurally
$conn = mysqli_connect("localhost","root","","anka");
if(!$conn){
echo "Connection error" . mysqli_connect_error("localhost","root","","anka");
}
$participantid = 0;
$participantid = fopen('performancerequests.txt',"r") or die("Request not taken.");
$content = fread($participantid, filesize("performancerequests.txt"));
file_put_contents("performancerequests.txt","");
//should return rank, points, products left(quantity)
$sql1 = "SELECT points from participants where id='$content'";
$sql2 = "SELECT quantity from products where participant_id='$content'";
$sql3 = "SELECT points, ROW_NUMBER() OVER( ORDER BY points ) RowNumber from participants where id='$content'";
$result1 = mysqli_query($conn, $sql1);
$result2 = mysqli_query($conn, $sql2);
$result3 = mysqli_query($conn, $sql3);
while ($row1 = $result1->fetch_assoc()) {
$points = $row1['points'];
}
while ($row2 = $result2->fetch_assoc()) {
$quantity = $row2['quantity'];
}
while ($row3 = $result3->fetch_assoc()) {
$rank = $row3['rank'];
}
$handle = fopen("performance.txt", "w") or die("File does not exist.");
if(fwrite($handle,$content." "."Points: ".$points." "."Quantity: ".$quantity." "."Rank: ".$rank) == false ){
echo "Error Writing.";
}
?>
The code for the tables in question are also as follows:
CREATE TABLE participants (
id bigint(20),
name varchar(255),
password varchar(255),
product varchar(255),
DOB date,
points int(11),
PRIMARY KEY(id)
);
CREATE TABLE products (
id bigint(20),
name varchar(255),
quantity varchar(255),
rate varchar(255),
description varchar(255),
FOREIGN KEY(participant_id) REFERENCES participants(id),
PRIMARY KEY(id)
);
But when I test and run it, it returns the warning that there’s an undefined array key, for the variable $rank. And in the text file, the rank bit is also left blank.
Is there a way I could solve this?