How to select a timestamp using date in mysql and php - php

I want to query if there is a particular timestamp for a day using the distinct dates that are present in a table and so far I have come up with this code
$mysqli = new mysqli('localhost', 'root', 'rolemodel', 'dashboard');
$dates = "SELECT DISTINCT DATE_FORMAT(mytimestamp, '%Y-%m-%d') FROM ".$this->table_name." ORDER BY DATE(mytimestamp) ASC;";
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$rowsdata = $mysqli->query($dates); // get number of rows
$num_rows_date = $rowsdata->num_rows;
for ($i = 0; $i<=$num_rows_date; i++) {
$current_query = "SELECT mytimestamp FROM ".$this->table_name." WHERE mytimestamp =".$rowsdata[$i]; //not sure on what to use here
}
I am confused on how to query the exact timestamp which is supposed to be the particular day along with the 00:00:00
Example 2014-06-02 00:00:00
Hope my question makes sense.

try:
//$rowsdata = $mysqli->query($dates); // get number of rows
//$num_rows_date = $rowsdata->num_rows;
//for ($i = 0; $i<=$num_rows_date; i++) {
//This is usually the way you want to do this with plain old arrays
if($result = $mysqli->query($dates)){
while($row = $result->fetch_row()){
$current_query = "SELECT mytimestamp FROM ".$this->table_name." WHERE DATE(mytimestamp) ='".$row[0] . "'";
//do something awesome here ...
}
}
See the docs for the DATE function.
BUT ... it looks like you're just querying the same table you pulled the dates from in the first place, so you're guaranteed to find timestamps with those dates. Not sure what you're ultimately after, but you may be able to do it with a single query.

Related

How to generate an employee id with year of registration in PHP?

I need to generate an employee id for eg: E13001, the number should be auto incremented, and when the next year comes, it should be E14001(year is 2014).It should again start from 001 for every year. I have tried with yy format but the sequence number continues from the previous year. How i suppose to my change the sequence number generation ? help me. Thanks in Advance.
Well, the auto-increment on a column in MySQL will not work except for integers. Also it will increment with each field.
To do what you want, you need to separate the id and year in tow (for the mysql table). So you get a table with id,emp_id(should not be managed by the db, no AUTO-INCREMENT) and year. Then select max(emp_id) where year ='2014', so when adding a new employee you will increment that value. To get the employe id the way you need it you should merge the tow columns into one and dump the first tow chars from the year.
You should know that this then becomes a distributed e_id, and every time you need to work with it you will need to do extra work. But you keep the format you need.
Hope this helps you get an idea of how it can be done.
Hi you can try in the following way
<?php
$connection = mysqli_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("test",$connection)
or die("no connection to db");
$query3 = "SELECT * FROM simple";
$result3 = mysqli_query($query3);
$count = mysql_num_rows($result3);
if($count == 0)
{
$year = date('y');
$seq = 1;
$a = sprintf("%04d", $seq);
$emp_id = E.$year.$a;
$query= "INSERT INTO simple (emp_id) VALUES ('$emp_id')";
$result=mysqli_query($query)or die(mysql_error());
}
else
{
$query2 = "SELECT * FROM simple ORDER BY id DESC LIMIT 1";
$result2 = mysqli_query($query2);
$row = mysql_fetch_array($result2);
$last_id = $row['emp_id'];
$rest = substr("$last_id", -4);
$insert_id = "$rest" + 1;
echo $ars = sprintf("%04d", $insert_id);
$year = date('y');
$emp_id = E.$year.$ars;
$query= "INSERT INTO simple (emp_id) VALUES ('$emp_id')";
$result=mysqli_query($query)or die(mysql_error());
}
Hope this code helps you.

php mysql finding rows older than a day from date feald

Here is how it should be:
search the database for uploads older than a day.
output the id's for the uploads in a list.
here is what I have so far:
<?php
include 'config.php';
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);
$timestamp = strtotime("-1 days");
$efined = mysql_query("SELECT * FROM uploads WHERE timestamp < '$timestamp'");
$efound = mysql_query($efined);
$enum = mysql_numrows($efound);
$ecount = 0;
echo $enum.'records were found.';
while ($ecount < $enum) {
$euid = mysql_result($efound,$ecount,"uid");
echo $euid.'<br>';
$ecount++;
}
mysql_close($connect);
?>
currently, this outputs nothing, when there is a record 3 days old.
How would I spesify the date format? in my database, it looks like this: 2013-04-02.
thanks for any help,
josh.
this
$enum=mysql_numrows($efound);
should be
$enum=mysql_num_rows($efound);
EDIT.
try your sql like that
where timestamp < date('now', '-1 days')
edit:
you are defining two mysql_query
change this
$efined = mysql_query("SELECT * FROM uploads WHERE timestamp < '$timestamp'");
to
$efined = "SELECT * FROM uploads WHERE timestamp < '$timestamp'";
Your mistake...
$efined = mysql_query("SELECT * FROM uploads WHERE timestamp < '$timestamp'");
$efound=mysql_query($efined);
$enum=mysql_numrows($efound);
There must be only one query and wrong num_rows name...
$sql= "SELECT * FROM uploads WHERE timestamp < '$timestamp'";
$efound = mysql_query($sql);
$enum = mysql_num_rows($efound);
P.S. The old myslq functions support ends at PHP 5.4 . Its good for you to start using myslqi or PDO mysql !

Error using oracle sequence in php

Every time when I'm running the following PHP code I'm getting output
Booking Confirmed! Congratulation. Your Booking Id is: 6
for successful booking.
The bookingid is a sequence and then I'm getting that booking id form the inserted values to get the current value of the sequence. So I don't know what I'm doing wrong.
<html><body>
<?php
$con = oci_connect("system", "password", "localhost/XE");
if (!$con) {
$m = oci_error();
exit('Connect Error ' . $m['message']);
}
$thid = $_GET["hid"];
$trno = $_GET["rno"];
$tgid = $_GET["gid"];
$sd = $_GET["sdate"];
$ed = $_GET["edate"];
$dchange = "ALTER SESSION SET NLS_DATE_FORMAT= 'YYYY-MM-DD'";
$stid1 = oci_parse($con,$dchange);
oci_execute($stid1);
$c1 = "SELECT * FROM B WHERE HOTELID = '$thid' AND ROOMNO = '$trno' AND ((STARTDATE < '$sd' AND ENDDATE > '$sd') or (STARTDATE < '$ed' AND ENDDATE > '$ed') or (STARTDATE >= '$sd' AND ENDDATE <= '$ed'))";
$c2 = oci_parse($con, $c1);
oci_execute($c2);
$row = oci_fetch_row($c2);
if(!$row)
{
$temp = "INSERT INTO B VALUES(bno.nextval,'$thid','$trno','$tgid','$sd','$ed')";
$stid = oci_parse($con,$temp);
oci_execute($stid);
oci_free_statement($stid);
//$c7 = "SELECT bookid FROM B WHERE HOTELID = '$thid' AND ROOMNO = '$trno' AND GUEStID = '$tgid' AND STARTDATE = '$sd' AND ENDDATE = '$ed'";
//printf("<h3>Booking Confirmed! Congatulation. </h3>") ;
**$c8 = oci_parse($con, "SELECT MAX(BOOKID) FROM B");
oci_execute($c8);
printf("<h3>Booking Confirmed! Congratulation. Your Booking Id is: %u</h3>", $c8);**
}
else
printf("<h3>Booking already exist. </br>Please try with another search.</h3>");
?>
</n> <form action="index.php"><input type="submit" value="BACK" />
</form>
</BODY>
a few suggestions for you.
you should be using bind variables and not splicing literals in your SQL statements (unless you want the performance of your DB to degrade as you fill the shared pool up).
SELECT MAX(BOOKID) FROM B is the wrong and unsafe way to get the booking id. as if 2 sessions in parallel made a booking, you could get wrong results (you can also get wrong results if the max id in the table is higher than the current sequence value). instead do select bno.currval from dual or use the returning clause as part of the insert (assuming PHP works with that)
on your insert you should specify the column names for good practice . i.e do INSERT INTO B (BOOKID, HOTELID, ROOMNO, STARTDATE, ENDDATE) VALUES(bno.nextval....

I need to output my Access table to a website using PHP

Alright, I have no problem outputting multiple lines of data out of the database, I just have no idea how to get it out in the format I want.
Here's my basic issue.
<tr>
<?php
//make connection to database, bail if no connection
$connection = odbc_pconnect('sitedata','','');
if (!$connection) { exit("Connection Failed: " . $connection); }
//retrieve relevant data
$Date = "SELECT EntryDate AS DT FROM TimeSheet WHERE EmployeeID = 'AA01'";
$rs = odbc_exec($connection, $Date);
//output data
while(odbc_fetch_row($rs)) {
$Line1 = odbc_result($rs, 'DT');
printf("<td><center>%s</center></td>", $Line1);
}
?>
</tr>
What this will do is output a one row table, with each new date creating a new column.
Now then, this is sort of useless to me as these need to be output into a single column, where each new datavalue is the beginning of a row. After this, I need to expand the table width wise, adding new data values such as:
$Time = "SELECT TotalTime as EN FROM TimeSheet WHERE EmployeeID = 'AA01'";
$JobName = "SELECT JobName as JN FROM TimeSheet WHERE EmployeeID = 'AA01'";
Where Time is it's own column, JobName is it's own column, etc.
How exactly do I do this? There obviously must be a way, I'm just new at this. Any help is greatly, greatly appreciated!
You need to get the fields in one SQL statement...
SELECT EntryDate AS DT, TotalTime as EN, JobName as JN
FROM TimeSheet WHERE EmployeeID = 'AA01';
With that you can do exactly what you are doing now except build a full row instead of columns.
<?php
//make connection to database, bail if no connection
$connection = odbc_pconnect('sitedata','','');
if (!$connection) { exit("Connection Failed: " . $connection); }
//retrieve relevant data
$Date = "SELECT EntryDate AS DT, TotalTime as EN, JobName as JN FROM TimeSheet WHERE EmployeeID = 'AA01';";
$rs = odbc_exec($connection, $Date);
//output data
while(odbc_fetch_row($rs)) {
$DT = odbc_result($rs, 'DT');
$EN = odbc_result($rs, 'EN');
$JN = odbc_result($rs, 'JN');
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>", $DT, $EN, $JN);
}
?>

Hide things from the past in PHP/MySQL

I have several activities in my database, but I would like to hide the ones from the past, so I modified my MySQL request:
$sql = "SELECT * FROM tblAgenda WHERE date <= CURDATE() order by date ASC";
But it doesn't do a thing except giving errors. What's wrong?
It seems like you're not getting any results, so that is throwing errors. You always need to check for results before looping, you cannot just assume that every query will return something.
Also, if you want things from the present/future, your comparison operand is backward:
$sql = "SELECT * FROM tblAgenda WHERE date >= CURDATE() order by date ASC";
Putting it together:
$sql = "SELECT * FROM tblAgenda WHERE date >= CURDATE() order by date ASC";
$result = mysql_query($sql);
if (!$result) {
// do something to handle zero results here
} else {
// do your usual while... loop
while ($row = mysql_fetch_assoc($result)) {
// code for each result row
}
}

Categories