I need to output my Access table to a website using PHP - 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);
}
?>

Related

How to update data when the expiration time ends

I have build an apps with Codeigniter and want to make cron job and use MySQL
i have a table 'order' and have field like this
order_id | order_expired_date
001 | 2018-11-12 10:03:33
and i have table 'order_payment' like this,
order_id | op_status
001 | pending
I have many fields in these two tables but only include those that have something to do with this question
i have code from php but not in model codeigniter
$result = mysql_query('UPDATE `'order_payment'`
SET op_status='expired'
WHERE
(UNIX_TIMESTAMP( now( ) ) - `order_expired_date`));
The question is how to change the status in the order_payment table to expire when the expiration time is up?
You may use following code, you need to modify code as per your requirements..
<?php
$servername = "xyz";
$username = "xyz";
$password = "xyz";
$dbname = "xyz";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);
$sql = "SELECT *,o.`order_id` as `oid` FROM `order` as o,`order_payment` as op WHERE o.`order_id`=op.`order_id` AND op.`op_status`='paid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$today = date('Y-m-d');
$expDt = date('Y-m-d',strtotime($row['order_expired_date']));
if($today==$expDt){
$updCltSql = "UPDATE `order_payment` SET `op_status`='expired' WHERE `order_id`='".$row['oid']."'";
$conn->query($updCltSql);
}
}
}
$conn->close();
?>
We always prefer core-php file as cron-job..
After plenty of research, this is what I came up with - hope it helps. In your example, I don't think you need to put single quotes around the table name order_payment, unless that is something unique to CodeIgniter.
$orderID = mysql_query("SELECT order_id FROM order"); // Get order IDs of order table, assuming it has the same list of order IDs as the order_payment table
$order_ids = array(); // Put in array
while ($row = mysql_fetch_array($orderID)) {
$order_ids[] = $row['order_id'];
}
foreach ($order_ids as $order_id) { // Iterating through order_id's of array in order to read same index/row number from both tables
$expirationDate = mysql_query("SELECT order_expired_date FROM order WHERE order_id='$order_id'");
$expire = date('M j Y g:i A', $expirationDate); // Converting MySQL timestamp to correct format for strtotime
$today = strtotime("now");
if($today >= $expire) // If past the expiration date
mysql_query("UPDATE order_payment SET op_status='expired' WHERE order_id='$order_id'");
}

Is it possible to loop through each row of a table in a database

I'm trying to loop through each row of a table in a database, then once I'm on a particular row get the value of a certain column. Is this possible? I've done a couple Google searches but nothing really concrete. I try using the mysqli_fetch_array() function but when I do I get the results of a column. I want to target each row. The code I have so far gets me the "nid" column. That's not what I want. I want to iterate through each row.
<?php
$serverName = "localhost";
$username = "user1";
$password = "sp#99#1";
$databaseName = "developer_site";
// Connection
$connection = new mysqli($serverName, $username, $password, $databaseName);
// Check Connection
if ($connection->connect_error) {
die("Connection failed:" . $connection->connect_error);
} // line ends if statement
$queryNodeRevision = "SELECT nid FROM node_revision";
// line above creates variable $queryNodeRevision > selects column "nid" from table "node_revision"
$results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
while ($row = mysqli_fetch_array($results)) {
echo "NID: ";
echo $row['nid'];
echo "<br/>";
}
?>
You can select rows by a certain condition with an SQL query alone and also select all columns.
SELECT * FROM node_revision where condition;
condition could be anything. For example another_row = 'something'.
But if, for some reason, you need to process the nid inside your php script to know if that row is the "particular" one you're searching, then you just select all the columns, or the ones you need.
$queryNodeRevision = "SELECT nid, col1, col2 FROM node_revision";
$results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
while ($row = mysqli_fetch_array($results)) {
if (condiiton) {
echo "Particular: ".$row['nid']
." ".$row['col1']
." ".$row['col2'];
}
}
condition could be something like $row['nid'] == 123 for example.
Hope it helps.

Copy 1 sql table to another with some additional data

I have this code:
<?php
//MYSQL
$dbserver="..."; //adresa MySQL
$dblogin="..."; //jméno uživatele MySQL
$dbheslo="..."; //heslo MySQL
$dbnazev="..."; //název databáze MySQL
$mysqli = new mysqli($dbserver, $dblogin, $dbheslo, $dbnazev);
if ($mysqli->connect_error) {
die('Nepodařilo se připojit k MySQL serveru (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$mysqli->query("SET NAMES 'utf8'"); // nastavíme kódování MySQL
while($row = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL")->fetch_assoc()){
$tempid = $row['tempid'];
$jmeno = $row['jmeno'];
$prijmeni = $row['prijmeni'];
$email = $row['email'];
$bydliste = $row['bydliste'];
$souhlas = "on";
$aktuality = "on";
$timestamp = time();
$hash = md5("77c0a83besxxxcg1a190ab90d".time().$tempid.rand(10000000, 99999999));
$poznamky = "import19052018";
$vloz ="INSERT into `potvrzenotest` set jmeno='".$jmeno."', prijmeni='".$prijmeni."', bydliste='".$bydliste."', email='".$email."', souhlas='".$souhlas."', aktuality='".$aktuality."', timestamp='".$timestamp."', hash='".$hash."', poznamky='".$poznamky."';";
$result=$mysqli->query($vloz);
$cas = date('H:i');
echo '['.$cas.'] ID '.$tempid.' imported.', PHP_EOL;
}
mysqli_close($mysqli); .
?>
I have one table with some data and I have to copy it to another table with some additional data (like hash etc.).
When I run the code above, I got this:
[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.
So, only 1 row is being copied.
Could you please help me, where is the problem?
while($row = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL")->fetch_assoc()){ ... }
This loop will never end. And it will fetch the first row again and again.
You should execute the query outside the loop:
$selectResult = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL");
while ($row = $selectResult->fetch_assoc()) { ... }
As a side note: Consider to use a prepared statement for your INSERT statement in the loop. This will prevent SQL syntax errors if some of the values may contail special characters like '. If you run them in one transaction, you might even improve the performance.

How to get the sum in a column using MySQL and PHP

I've been searching for the better part of five days (no joke, don't want to bother you guys for nothing), but I can't seem to be getting this right. I want to display the total amount of a column (tech) on the screen using a function in PHP. Don't worry, the $day part works in other functions. I don't think it is part of the problem. Thank you in advance.
function calc_tech($day){
include("connect.php"); // include database connection
$res = $mysqli -> query(" SELECT * FROM sales WHERE day = $day ") or die($mysqli->error); // Fetch data from the table
while($val = $res -> fetch_array()){
$tech_total = $val[tech] += $val[tech];
}
echo $tech_total; // Echo the result
//echo "42";
$res -> free(); // Free the query results
$mysqli -> close(); // Close mysqli object
}
My database table looks like this:
You need to make the SUM() the column in the query :
function calc_tech($day){
include("connect.php"); // include database connection
$res = $mysqli -> query(" SELECT SUM(tech) as sum FROM sales WHERE day = $day ") or die($mysqli->error); // Fetch data from the table
$val = $res -> fetch_array();
$tech_total = $val['sum'];
echo $tech_total; // Echo the result
}
1) '$day' should be enclosed by single quotes .
FOR SUM of column name query should be like this
"select sum(tech) as Toatal from sales where day='$day';"

How to select a timestamp using date in mysql and 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.

Categories