YII2 get the date after 10 years and 5 years - mysql

Here is the table structure.
tbl_staff
firstname varchar(50)
midname varchar(50)
lastname varchar(50)
...
tbl_staff2
first_day_of_service date
...
Here is the scenario.
For the first 10 years of stay of the staff in organization, he/she will be given a loyalty award and then every 5 years after he/she receives the first 10 years.
My initial code will just get the name of staff and the date of first day of service.
public function actionGet_loyalty() {
$query = (new \yii\db\Query())
->select(['firstname', 'midname', 'lastname', 'first_day_service'])
->from('tbl_staff')
->limit(100)
->offset(0)
->orderBy('first_day_service')
->leftJoin('tbl_staff2', 'tbl_staff2.staff_id = tbl_staff.id');
$command = $query->createCommand();
$data = $command->queryAll();
$string = "<table class='table table-striped'><tr><th>No.</th><th>Name</th><th>Date to Receive Loyalty Award</th></tr>";
$i = 1;
foreach ($data as $row) {
$firstname = $row['firstname'];
$midname = $row['midname'];
$lastname = $row['lastname'];
//$string.=$row['first_day_service'];
$string.="<tr>"
. "<td>$i</td>"
. "<td>" . $firstname . " " . $midname[0] . ". " . $lastname . "</td>"
. "<td>" . $row['first_day_service'] . "</td>"
. "</tr>";
$i++;
}
$string.="</table>";
return $string;
}
How to display all the name of staff and the date he/she will receive the award in increasing order.
Or any mysql query that will sort the name according the date?

If you want to do it via SQL query, in MySQL you can add date using DATE_ADD function:
DATE_ADD(date, INTERVAL 5 YEAR)
Check this similar question.
Or via PHP using DateTime modify method:
$dateTime = new DateTime($date);
$dateTime->modify('+ 5 years')->format('Y-m-d H:i:s');

Related

how to fetch with an AM/PM time_format from any database

I want to fetch data with AM/PM format, but before that, i need to choose a database to make this.
$event_name='database_name';
I also using INNER JOIN .
Can anybody know how to format time in this case?
$event_name='database name';
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());}
$sql = "SELECT $event_name.student_login.LOG_IN, $event_name.student_login.TIME_FORMAT(LOG_IN,"%r"), event_attendance.student_data.* FROM event_attendance.student_data
inner JOIN $event_name.student_login ON $event_name.student_login.SERIAL = event_attendance.student_data.SERIAL order by $event_name.student_login.LOG_IN DESC";
//using (SERIAL)
$result = mysqli_query($conn, $sql);
$i = 1;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<td width='2'><center><B>".$i; $i++; echo"</B></center></td>";
echo "<td>" . $row["ID"]."</td>";
echo "<td>" . $row["LASTNAME"].", ". $row["FIRSTNAME"]." ". $row["MIDDLENAME"]."."."</td>";
echo "<td><center>" . $row["COURSE"]."</center></td>";
echo "<td><center>" . $row["YEAR"]."</center></td>";
echo "<td><center>" . $row["LOG_IN"] ."</center></td>";
echo "<td><center>" . $row["DATE"]."</center></td><tr>";
}
}
my code didn't work with this.
$event_name.student_login.TIME_FORMAT(LOG_IN,"%r")
TIME_FORMAT is an in-built MYSQL function and not a function of the table $event_name.student_login so using $event_name.student_login.TIME_FORMAT(LOG_IN,"%r") is wrong
Secondly, your query is in double quotes so you cannot directly use another double quote in the query string. So you need to change $event_name.student_login.TIME_FORMAT(LOG_IN,"%r") to TIME_FORMAT($event_name.student_login.LOG_IN, '%r')
Try this
$sql = "SELECT $event_name.student_login.LOG_IN, TIME_FORMAT($event_name.student_login.LOG_IN, '%r'), event_attendance.student_data.* FROM event_attendance.student_data inner JOIN $event_name.student_login ON $event_name.student_login.SERIAL = event_attendance.student_data.SERIAL order by $event_name.student_login.LOG_IN DESC";
or the cleaner version using alias
$sql = "SELECT sl.LOG_IN, TIME_FORMAT(sl.LOG_IN, '%r'), sd.* FROM event_attendance.student_data sd inner JOIN $event_name.student_login sl ON sl.SERIAL = sd.SERIAL order by sl.LOG_IN DESC";

PDO Statement accepting INDEX [0], [1], [2], but not [fieldname1], [fieldname2], [fieldname3]

Beginner here trying out PDO/MySQL. I am coding a simple SELECT * and displaying the results. The code below works fine to display the contents.
$sql = 'SELECT * FROM names'; //fieldnames are id, name, email
$stmt = $db->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch())
{echo $row[0] . " | " . $row[1] . " | " . $row[2] . "<br/>";}
However, when I use the fieldname as the index instead of the number, I get a PHP notice:
Notice: Undefined index: name
Codes below does not work.
{echo $row['id'] . " | " . $row['name'] . " | " . $row['email'] . "<br/>";}
I have seen several tutorials where they have used fieldname as the index. Can someone provide some further clarification on when/how the fieldname can be used instead of the number for the index?

MySQL Multiple Simultaneous Queries From The Same Connection

I'm trying to run multiple queries on a MySQL server at the same time from PHP with the same database connection. I have run the SQL statement on the server itself but the response is as expected. I'm getting no SQL exceptions but just no SQL query response at all (the result is FALSE).
Is this because you can't query when another query is already active on the same connection?
The code loads data from the database table with people's names and last paid date information. This is displayed in a form and then the user can select members to update payments for. Then, the part I'm stuck on is the second query where the selected names are trying to find family_id for families.
Code snippet:
mysql_select_db($database_W3OITesting, $W3OITesting);
$yearnow = date("Y",strtotime('+1 year')).'-12-31';
$yearrecent = date("Y",strtotime('-1 year')).'-12-31';
$query_Recordset1 = "SELECT DISTINCT lname, fname, suffix, fcccall, members.member_id, MaxDateTime " .
"FROM members " .
"INNER JOIN " .
"(SELECT paid.member_id, MAX(paid.year) AS MaxDateTime " .
"FROM paid " .
"GROUP BY paid.member_id) groupedpaid ".
"ON members.member_id = groupedpaid.member_id " .
"Where (MaxDateTime < '$yearnow') AND ".
"(MaxDateTime >= '$yearrecent')" .
"ORDER BY lname, fname, suffix, fcccall";
$Recordset1 = mysql_query($query_Recordset1, $W3OITesting) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
//if a post is received handle it here
function getPostArray($array){
foreach ($array as $key => $value){
//search here for if this member is part of a family
//if not part of a family just update the payment
//record for the member with the value
try {
$query_Recordset3 = "SELECT lname, fname, suffix, `members`.member_id , `family`.member_id " .
"FROM members, family " .
"WHERE (`members`.member_id = `family`.member_id " .
"AND `members`.member_id = $key)";
echo $query_Recordset3. "<br>";
$Recordset3 = mysql_query($query_Recordset3, $W3OITesting);
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);
echo $totalRows_Recordset3 . "<br>";
echo "$key => $value";
if($totalRows_Recordset3==FALSE) {//Recordset3 is always FALSE
echo " Error - " . $row_Recordset3['lname'];
}
if($totalRows_Recordset3!=0) {
echo " - A Family";
} else {
echo " - Individual";
}
echo "<br>";
}
catch(Exception $e)
{
echo "Exception: $e";
die();
}
if(is_array($value)){ //If $value is an array, get it also
getPostArray($value);
}
}
}

changing date output from sql from yyyy/mm/dd to dd/mm/yyyy

Hello im trying to changing the date output from sql from yyyy/mm/dd to dd/mm/yyyy
the date wont get saved in mySQL when I enter the date via a datepicker on my form. (wil input only 0000-00-00)
If I enter the date manually into MySQL then It will only show the date of today.
Im using the folowing code:
$result = mysql_query("SELECT * FROM overboekingen")
while($row = mysql_fetch_array( $result )) {
echo '<td>' . $row['datum_overboeking'] = date('d-m-Y') . '</td>';
Just try with this type solution
$TodayDate = "2014-03-07"; //OR the date from database. It means the date you will get from database
$FormatedDate = date("d-m-Y", strtotime($TodayDate));
This way you can put the conditions
$result = mysql_query("SELECT * FROM overboekingen")
while($row = mysql_fetch_array( $result ))
{
$databaseDate = $row['YOUR DATABASE DATE FIELD NAME'];
$FormatedDate = date("d-m-Y", strtotime($databaseDate));
echo '<td>' . $row['datum_overboeking'] = $FormatedDate . '</td>';
}
Im one step further and I can now insert the data correctly in the database, only it doesnt show the date from mySQL correctly when I echo in in my webapplication when it pulls it out SQL again.
It seems this piece of code is not correct:
echo '<td>' . $row['datum_overboeking'] = strtotime($row), date('m-d-Y') . '</td>'
Just try with this procedure to display the date from database with required date format
$result = mysql_query("SELECT * FROM overboekingen")
while($row = mysql_fetch_array( $result ))
{
$databaseDate = $row['YOUR DATABASE DATE FIELD NAME'];
$FormatedDate = date("d-m-Y", strtotime($databaseDate));
echo '<td>' . $row['datum_overboeking'] = $FormatedDate . '</td>';
}
May this will help you
$TodayDate = "2014-03-07"; //OR the date from database. It means the date you will get from database
$FormatedDate = date("d-m-Y", strtotime($TodayDate));

MySQL Update Not Updating Certain Rows

Here is my double-minded query:
$Quest = "SELECT * FROM TOAWorkorders";
$FindTechResult = mysql_query($Quest, $cxn)
or die ('The easter bunny is watching you' . mysql_error());
while ($row = mysql_fetch_array($FindTechResult))
{
if (strpos($BBT, 0, 3) != 'Sys')
{
$IdNum = $row['IdNum'];
$BBT = $row['BBT'];
$BBTArray = explode("-", $BBT);
$TechNum = $BBTArray["0"];
$Title = $BBTArray["2"];
$Name = explode(" ", $BBTArray['1']);
$FirstName = $Name["0"];
$LastName = $Name["1"];
}
echo $TechNum . ' !! ' . $FirstName . ' !! ' . $LastName . ' !! ' . $Title . '<br>';
$Quest = "UPDATE TOAWorkorders SET TechNum = '$TechNum', FirstName = '$FirstName', LastName = '$LastName', Title = '$Title' WHERE IdNum = '$IdNum'";
$result = mysql_query($Quest, $cxn) or die(mysql_error());
}
Everything works for about 2/3s of the database. That leaves 33,000 rows that are not updated. I cannot find any difference between the data that works and the data that doesn't.
Since you're doing an UPDATE, and you say the rest of the code works (meaning, I hope, that you get 109,112 echo'ed results), it must be that the ID isn't being found (WHERE IdNum = '$IdNum').
Try preceding that command with "SELECT COUNT(*) from TOAWorkorders WHERE IdNum = '$IdNum'" and see if you get 33,000 zeros when the program runs. If you do, then you have missing IdNum values in your table.
If you don't, please provide details and I'll let you know.