|
|
|
สอบถามวิธีเขียน code php และ sql ครับ (มือใหม่เริ่มหัดครับ) |
|
|
|
|
|
|
|
รบกวนช่วยแนะนำด้วยครับเพิ่มเริ่มหัดเขียน PHP โจทย์ก็คือ ต้องการตรวจสอบเบอร์โทร ว่ามีการโทรเข้ามาย้อนหลังภายใน 7 วันหรือไม่
ถ้าให้ทำการ update ค่า filed RR7 เป็น Y
จาก code ที่ลองดูใช้เวลานนานมาก รบกวนช่วยแนะนำวิธี sql เพื่อลดเวลาให้ด้วยครับ
ขอบคุณครับ
ข้อมูล
ticketid product_id reportdate RR7
TT201412108210 959526295 11/12/2014 15:34
TT201412108217 959526295 11/12/2014 15:39
TT201412118754 916964482 24/12/2014 2:29
TT201501100308 858052292 1/1/2015 15:00
TT201501100313 972432618 1/1/2015 15:09
TT201501100345 863689905 1/1/2015 16:54
TT201501100414 954273678 1/1/2015 20:34
TT201501100678 866054776 2/1/2015 10:44
TT201501100704 902272552 2/1/2015 11:39
TT201501100748 972628654 2/1/2015 14:29
TT201501100844 853771687 2/1/2015 19:04
TT201501101217 887680628 3/1/2015 12:54
TT201501101343 882738089 3/1/2015 18:29
TT201501101418 909309756 3/1/2015 23:24
TT201501101429 840529119 4/1/2015 0:19
Code (PHP)
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "reportdb";
set_time_limit(300);
date_default_timezone_set('Asia/Bangkok');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ticketid, product_id, reportdate FROM sccd_rawdata where product_id != 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "TT No: " . $row["ticketid"]. " MSISDN : " . $row["product_id"]. " Reportdate : " . $row["reportdate"]. "<br>";
$sdate = date("Y-m-d",strtotime($row["reportdate"]));
$edate = date("Y-m-d",strtotime("+ 6 day",strtotime($row["reportdate"])));
echo $sdate." - ".$edate."<br>";
$sql1 = "SELECT ticketid, product_id, reportdate FROM sccd_rawdata where reportdate between '$sdate' and '$edate'";
$result1 = $conn->query($sql1);
if($result1->num_rows > 0 ){
while($row1 = $result1->fetch_assoc()) {
if ((($row["product_id"] = $row1["product_id"])) && (($row["ticketid"] != $row1["ticketid"]))){
echo $row["ticketid"]." ".$row["product_id"]." ".$row["reportdaet"]."<br>";
echo $row1["ticketid"]." ".$row1["product_id"]." ".$row1["reportdaet"]."<br>";
}
}
}
}
} else {
echo "0 results";
}
$conn->close();
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2015-09-02 09:16:59 |
By :
aaraikan |
View :
849 |
Reply :
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
$sql1="SELECT product_id FROM sccd_rawdata where reportdate between '$sdate1' and '$edate' "; // statement แค่นี้ก็พอครับ
$result1 = $conn->query($sql1);
if($result1){
echo "Successful"."<br>";
}else {
echo "ERROR ".$conn->error."<br>$sql1"; // บันทัดนี้จะได้รู้ว่าอะไร
}
|
|
|
|
|
Date :
2015-09-02 10:54:17 |
By :
NewbiePHP |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "reportdb";
set_time_limit(0);
date_default_timezone_set('Asia/Bangkok');
$RR = "Y";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ticketid, product_id, reportdate FROM sccd_rawdata where product_id != 0";
$result = $conn->query($sql);
if($result){
echo "Successful sql"."<br>";
}
else {
echo "ERROR"."<br>";
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$r = $r + 1;
//echo "select sql => TT No: " . $row["ticketid"]. " MSISDN : " . $row["product_id"]. " Reportdate : " . $row["reportdate"]. "<br>";
$sdate = date("Y-m-d",strtotime($row["reportdate"]));
$edate = date("Y-m-d",strtotime("+ 6 day",strtotime($row["reportdate"])));
echo "Date: ".$sdate." - ".$edate."<br>";
$sql1 = "SELECT ticketid, product_id, reportdate FROM sccd_rawdata where reportdate between '$sdate' and '$edate'";
$result1 = $conn->query($sql1);
if($result1){
echo "Successful sql1"."<br>";
}
else {
echo "ERROR 1"."<br>";
}
if($result1->num_rows > 0 ){
while($row1 = $result1->fetch_assoc()) {
if ((($row["product_id"] == $row1["product_id"])) && (($row["ticketid"] != $row1["ticketid"]))){
echo "found record rr"."<br>";
echo $row["ticketid"]." ".$row["product_id"]." ".$row["reportdate"]." ".$row["RR7"]."<br>";
echo $row1["ticketid"]." ".$row1["product_id"]." ".$row1["reportdate"]." ".$row1["RR7"]."<br>";
echo "============================================================="."<br>";
$i = $i + 1;
}
}
}
}
} else {
echo "0 results";
}
$conn->close();
echo Total Record : $r."<br>";
echo "Total Record RR7 : "$i;
?>
output ที run ได้ครับ
Date: 2015-08-27 - 2015-09-02
Successful sql1
found record rr
TT201508147547 929462001 2015-08-27 11:35:00
TT201508147198 929462001 2015-08-27 08:55:00
=============================================================
Date: 2015-08-27 - 2015-09-02
Successful sql1
Date: 2015-08-27 - 2015-09-02
Successful sql1
found record rr
TT201508147716 941858650 2015-08-27 13:10:00
TT201508147151 941858650 2015-08-27 08:15:00
=============================================================
Date: 2015-08-27 - 2015-09-02
Successful sql1
24034
3293
รบกวนช่วยแนะนำให้ด้วยครับ ถ้าต้องการเปลี่ยน 2 บรรทัดล่างนี้ให้เป็นคำสั่ง update ให้ทั้ง 2 record field RR7="Y" ต้องใช้คำสั่งยังไงครับ
echo $row["ticketid"]." ".$row["product_id"]." ".$row["reportdate"]." ".$row["RR7"]."<br>";
echo $row1["ticketid"]." ".$row1["product_id"]." ".$row1["reportdate"]." ".$row["RR7"]."<br>";
ขอบคุณครับ
|
|
|
|
|
Date :
2015-09-02 15:57:29 |
By :
aaraikan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32.$sql1 = "SELECT ticketid, product_id, reportdate FROM sccd_rawdata where reportdate between '$sdate' and '$edate'";
เลียนแบบจากบันทัดบน
Code (PHP)
$sqlUD = "update sccd_rawdata where reportdate between '$sdate' and '$edate'";
|
|
|
|
|
Date :
2015-09-02 20:12:28 |
By :
NewbiePHP |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
|
|
|
|
|
Date :
2015-09-03 07:31:33 |
By :
aaraikan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|