วันจันทร์ที่ 28 ตุลาคม พ.ศ. 2556

PHP curl service send xml data



<?php


  $server ="http://yourservername/getservicexml.php";
  $xmldata ="<tests>
    <control>
        <module>Modulename</module>
    </control>
    <test>
        <sn>5555</sn>
        <ticket>555</ticket>
        <test_station>5555</test_station>
        <tester_name>testername</tester_name>
        <user>user</user>
        <result>F</result>
        <resource_name>resourcename</resource_name>
        <test_log>
            <![CDATA[
       Log message or your data text data field
            ]]>
        </test_log>
    </test>
</tests>
";


  function sendRequest($requestXML,$server)
   {
   // $server = 'http://127.0.0.1/exam/php_xml/receive_postxml.php';
    $headers = array(
    "Content-type: text/xml",
    "Content-length: ".strlen($requestXML),
    "Connection: close"
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $server);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 200);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML);
 // curl_setopt($ch, CURLOPT_POSTFIELDS, "?XML=TT&password=".$password."&etc=etc");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $data = curl_exec($ch);
    echo $data;
    if(curl_errno($ch)){
        $errmes =  curl_error($ch);
        $textlog = date("d/M/Y H:i:s",time())."  ".$errmes." something went wrong..... try later\n";
        //saveaction(ODCSENDLOG,$textlog);
        echo $textlog;
        exit();
    }else{
        curl_close($ch);
    }
   
   if(!$data)
    {
     return false;
    }
    return $data;
  }

  sendRequest($xmldata,$server);

  ?>

/*------------------------------------------*/

// Received data getservicexml.php

<?php

if (!function_exists('getallheaders'))
{
    function getallheaders()
    {
           $headers = '';
       foreach ($_SERVER as $name => $value)
       {
           if (substr($name, 0, 5) == 'HTTP_')
           {
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
           }
       }
       return $headers;
    }
}
 $arr1 = getallheaders(); //apache_response_headers();
 $txt = "";
 foreach($arr1 as $key => $value)
 {
  $txt .= $key.":".$value."\n";
 }

 if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
    $xml = $GLOBALS["HTTP_RAW_POST_DATA"];
    echo $xml;
   
  }

 $txt .= $xml;

 $handle=fopen("testxmlheader.log","a+");
 fwrite($handle,$txt."\n");
 fclose($handle);

  function xmlhead_savelog($text)
  {
  $xmlstr="<?xml version=\"1.0\" ?>";
  $xmlstr.="<tests>";
  $xmlstr.="<control>";
  $xmlstr.="<module>SAVELOGFILE</module>";
  $xmlstr.="</control>";
  $xmlstr.=$text;
  $xmlstr.="</tests>";
  return $xmlstr;
  }

?>

วันพุธที่ 24 กรกฎาคม พ.ศ. 2556

Resize images using PHP

if ($ext =="jpg" or $ext =="jpeg") {
$ori_img = imagecreatefromjpeg($photo);
} else if ($ext =="png") {
$ori_img = imagecreatefrompng($photo);
} else if ($ext =="gif") {
$ori_img = imagecreatefromgif($photo);
}

$ori_size = getimagesize($photo);
$ori_w = $ori_size[0];
$ori_h = $ori_size[1];
  if($ori_w >720)
  {
if ($ori_w>=$ori_h) {
$new_w = 720;
$new_h = round(($new_w/$ori_w) * $ori_h);
} else {
$new_h =720;
$new_w = round(($new_h/$ori_h) * $ori_w);
}
$new_img= imagecreatetruecolor($new_w, $new_h);
imagecopyresized( $new_img, $ori_img,0,0,0,0,$new_w, $new_h,$ori_w,$ori_h);

if ($ext =="jpg" or $ext =="jpeg") {
imagejpeg($new_img,"../images/$filename");
} else if ($ext =="png") {
imagepng($new_img,"../images/$filename");
} else if ($ext =="gif") {
imagegif($new_img,"../images/$filename");
}

imagedestroy($ori_img);
imagedestroy($new_img);
}

วันจันทร์ที่ 21 พฤษภาคม พ.ศ. 2555

Mysql command


# [mysql dir]/bin/mysql -h hostname -u root -p
Create a database on the sql server.

mysql> create database [databasename];
List all databases on the sql server.

mysql> show databases;
Switch to a database.

mysql> use [db name];
To see all the tables in the db.

mysql> show tables;
To see database's field formats.

mysql> describe [table name];
To delete a db.

mysql> drop database [database name];
To delete a table.

mysql> drop table [table name];
Show all data in a table.

mysql> SELECT * FROM [table name];
Returns the columns and column information pertaining to the designated table.

mysql> show columns from [table name];
Show certain selected rows with the value "whatever".

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";
Show all records containing the name "Bob" AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';
Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;
Show all records starting with the letters 'bob' AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';
Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;
Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";
Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];
Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];
Sum column.

mysql> SELECT SUM(*) FROM [table name];
Join tables on common columns.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;
Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;
Change a users password from unix shell.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;
Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
Set a root password if there is on root password.

# mysqladmin -u root password newpassword
Update a root password.

# mysqladmin -u root -p oldpassword newpassword
Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;
Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;
To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = 'whatever';
Update database permissions/privilages.

mysql> flush privileges;
Delete a column.

mysql> alter table [table name] drop column [column name];
Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);
Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);
Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);
Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);
Delete unique from table.

mysql> alter table [table name] drop index [colmn name];
Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
Dump all databases for backup. Backup file is sql commands to recreate all db's.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
Create Table Example 1.

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));
Create Table Example 2.

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');

SQL Command part2 (STOU 96408)


---กิจกรรมที่ 5.1------------

-- 2.DML
/*
update,delete,select
*/

select * from customer;

--การแก้ใขข้อมูลในตาราง

update customer set cus_lname='pothong' where cus_code='10010';
update employee set salary=salart*1.5;


select * from pilot;
select * from employee;

--การลบข้อมูลในตาราง

delete from employee where emp_fname like 'k%';
----------------------------------------------------

--5.1
--select * from employee where emp_fname='jitrlada';
delete from employee where emp_fname='Jitrlada' AND emp_lname='Polnimit';
delete from employee where emp_num='106';
--5.2

delete from customer where cus_code='10018';

--5.3

update customer set cus_phone='988-2588' where cus_fname='Jittinan' AND cus_lname='pothong';

--5.4

update customer set cus_balance=45650.75 where cus_fname='Pong' AND cus_lname='Smith';

--5.5
update pilot set PIL_MED_TYPE='2' where pilot.emp_num=employee.emp_num AND employee.emp_fname='George';
update pilot set PIL_MED_TYPE='1' where pilot.emp_num = (select emp_num from employee where employee.emp_fname='George');



create database aircraft52


--5.2 select

select * from employee;
select emp_num,emp_fname,emp_lname from employee order by emp_fname;
select emp_num,emp_fname,emp_lname from employee order by 2;
select emp_num,emp_fname,emp_lname from employee order by 2 desc; --เรียงจากมากไปน้อย
select cus_fname,cus_lname,cus_balance from customer where cus_balance > 30000 order by 3 desc;

select cus_fname,cus_lname,cus_balance from customer
where cus_balance between 20000 and 30000 order by 3 desc;


--5.2.3
/*  แสดงหมายเลขสัญญาเช่า จุหมายปลายทางของเครื่องบินที่ไปลงที่ atlanta และจำนวนชั่วโมงที่พักเครื่อง
 มากกว่าหรือเท่ากับ 3.5
*/


select char_num,char_destination from charter
where char_destination = 'ATL' and char_hours_wait >=3.5;

--5.2.1

select * from charter order by char_date desc;

--5.2.2

select * from charter where char_date between '02/13/2003' and '02/14/2003' ;

--5.2.3
select char_date,char_destination,char_distance,char_hours from charter
where ac_num='2778V';

--5.2.4
select cus_fname,cus_lname,cus_phone,cus_balance from customer
where cus_balance > 0;

--5.2.5

select emp_title,emp_fname,emp_lname from employee,pilot
where pilot.emp_num=employee.emp_num and pilot.pil_license='COM' ;


/************************
ให้แสดง หมายเลขสัญญาเช่า ชื่อลูกค้า ของลูกค้าที่ชื่อ jittinan
*************************/
select char_num,cus_fname from charter,customer
 where charter.cus_code=customer.cus_code and cus_fname = 'jittinan';


--5.2.6

select char_date,char_destination,ac_num,cus_fname,cus_lname,cus_areacode,cus_phone from charter,customer
 where charter.ac_num='2778v' and charter.cus_code=customer.cus_code;

--5.2.7
 select cus_fname,cus_lname,cus_areacode,cus_phone from customer,charter
  where customer.cus_code=charter.cus_code and charter.ac_num='2778V' and customer.cus_balance > 30000;

--5.2.8

select char_date,cus_fname,cus_lname,cus_areacode,cus_phone from charter,customer
  where charter.cus_code=customer.cus_code and cus_fname like '_i%';

--5.2.9

select char_num,char_date,char_destination,char_hours,mod_name,charter.ac_num  from charter,aircraft,model
 where charter.ac_num = aircraft.ac_num and aircraft.mod_code=model.mod_code and co_pil_num <> '';

-- 5.2.10

select char_date,ac_num,emp_fname,emp_lname from charter,employee,pilot
  where charter.ac_num='2778V' and charter.pil_num=pilot.pil_num and pilot.emp_num=employee.emp_num;
-- 5.2.11

select char_date,char_num,charter.ac_num,mod_name from  charter,aircraft,model
 where charter.ac_num = aircraft.ac_num and aircraft.mod_code=model.mod_code and char_date >= '02/15/2003';

--5.2.12

select char_date,char_destination,charter.ac_num,mod_chg_mile,char_distance,cus_fname,cus_lname
 from charter,customer,aircraft,model
   where char_destination='ATL' and charter.ac_num = aircraft.ac_num and aircraft.mod_code=model.mod_code
           and charter.cus_code=customer.cus_code;

--5.2.13


select char_date,charter.ac_num,emp_fname,char_hours,char_fuel,charter.char_fuel/char_hours as fuelPerHours from charter,pilot,employee,aircraft
  where char_date >= '02/15/2003' and charter.ac_num=aircraft.ac_num
   and charter.pil_num=pilot.pil_num and pilot.emp_num=employee.emp_num


--5.2.14

select char_date,char_distance,mod_chg_mile,mod_chg_mile*char_distance as mileageChange from charter,model,aircraft
 where char_date >='02/14/2003' and charter.ac_num=aircraft.ac_num and aircraft.mod_code=model.mod_code;

--5.2.15 *******

select char_date,char_distance
     
  from charter,customer
    where char_date >='02/16/2003' and charter.cus_code=customer.cus_code;

--5.2.16

select avg(cus_balance) as SumOfBalance,
       min(cus_balance) as MinBalance,
       max(cus_balance) as MaxBalance,
       sum(cus_balance) as SumBalance
from customer;

--5.2.17
select ac_num,sum(char_distance)as'Number of Flow',
       count(ac_num) as 'Total Distanct',sum(char_hours)as 'Total Hours'
   from charter
    group by ac_num ;
   
   

--5.2.18

select char_destination,count(char_destination) as 'charter amout',min(char_hours)as'Minimum Hours'
   from charter
    group by char_destination order by count(char_destination) desc  ;

--5.2.19
select  ac_num,ac_ttel,mod_name,mod_chg_mile as 'change per mile'
  from aircraft,model
    where mod_chg_mile < 100 and aircraft.mod_code=model.mod_code ;


--5.2.20

 select ac_num,count(ac_num)as 'Number of Flow',max(char_hours)as 'MaxIMUM HOURS'
    from charter
      group by ac_num having count(ac_num) > 4;

SQL Command Part1


--1. DDL
--create database Testaircraft; --create database
-- การสร้าง Table
create table test1
 ( T_ID char(4) not null primary key,
   T_Name char(40) not null,
   T_salary int not null
  );
-- ดูรายละเอียด ของตาราง
sp_help test1;
---------------
create table test2
 ( T_ID char(4) not null,
   T_code char(3) not null,
   T_salary int not null,
   primary key (T_ID,T_code)
  );
sp_help test2;
-- สร้าง fk
create table test3
 ( T_ID char(4) not null primary key,
   T_code char(3) not null references test4(T_code),
   T_salary int not null
  );

create table test4
 ( T_code char(3) not null primary key,
   T_Name char(3) not null,
   T_salary int not null
  );
------------------------------------------
create table test11
 ( T_ID char(4) not null primary key,
   T_name char(4) not null
  );
create table test12
 ( T_Code char(3) not null primary key,
   T_name char(4) not null
  );
create table test13
 ( T_Des int not null primary key,
   T_name char(4) not null
  );
create table test5
 ( T_ID char(4) not null references test11(T_ID),
   T_Code char(3) not null references test12(T_Code),
   T_name char(3) not null,
   T_Des int not null references test13(T_Des),
   primary key(T_ID,T_Code)
  );
sp_help test5;
-------------------------------------
drop table test5;

------ เปลี่ยนชื่อตาราง--------
sp_rename 'test1','Exam1';
------ เปลี่ยนชื่อ arrtibute--------
sp_rename 'Exam1.T_name','T_surname';
-------------------------------------
drop table test11;

---- การเปลี่ยนแปลงแก้ใขโครงลร้าง Table

create table Ex1
( E_ID char(4) not null,
  E_name varchar(40) not null
);

alter table Ex1 add primary key(E_ID); -- add primary key in table ex1
alter table Ex1 add constraint PK_E_ID primary key(E_ID); -- add primary key in table ex1
alter table Ex1 drop constraint PK__Ex1__2C3393D0; -- add primary key in table ex1

sp_help Ex1;

alter table Ext add E_slary int; -- add colum E_slary value by int

--------------------------------------------------------------------------
-- DML data manipulation language
/*
 insert ,update ,delete,select
*/
 create table Ex2
 (
   --E_ID char(4) not null check(E_ID like 'E?[a-z][0-9]') primary key,
   E_ID char(4) not null check(E_ID like 'EA[0-9][0-9]') primary key,
   E_Name varchar(30) not null,
   E_BD datetime,
   E_salary int check(E_Salary > 5000)
 );

 drop table Ex2;


--การ เพิ่มข้อมูล
 insert into Ex2 values('EA01','Pirom konglerd','3/26/2011',50001);
 insert into Ex2 values('EA02','rom konglerd','3/26/2011',50001);
 insert into Ex2 (E_ID,E_Name)values('EA03','rom konglerd');

 set dateformat dmy; -- set format date
 select * from Ex2;
 update Ex2 set E_salary=6000;




-- DCL
/*
 grant revoke
 examp:
 create user pirom set password 123456


*/



วันศุกร์ที่ 21 ตุลาคม พ.ศ. 2554

PHP

PHP คือ อะไร

PHP คือ ภาษา Server Site Script ชนิดหนึ่ง หรือ สคริปที่ทำงานบนฝั่ง Server ซึ่ง Server ซึ่งมีรูปแบบการเขียนคล้ายกับภาษา C  จะประมวลผลและสร้างเป็น HTML ส่งมายังฝั่ง Client เพื่อแสดงผลผ่าน Web Browser

เริ่มต้น เขียนเว็บด้วย PHP ต้องมี 3 อย่างครับ

1. โปรแกรม Web Server ซึ่งเราต้องทำให้เครื่องเราเป็น Server ก่อน
     ในที่นี้ขอแนะนำ โปรแกรม Appserv นะครับ ซึ่งเหมาะสำหรับการพัฒนา เว็บด้วยภาษา PHP โดย 
     โปรแกรม Appserv ประกอบด้วย 
  • ตัวรัน สคริป PHP (ตัวแปรภาษา)
  • โปรแกรม Web Server (Apache)
  • โปรแกรม Mysql สำหรับสร้างฐานข้อมูล
  • โปรแกรม phpMyAdmin เป็นตัวจัดการฐานข้อมูล
      สามารถ Download ได้ ฟรี ใช้งานง่าย
2  คอมพิวเตอร์
3. โปรแกรม Editor เช่น Notepad, PS pad, Edit Plus, Dreamweaver

รูปแบบโครงสร้างพื้นฐานของ PHP
PHP เป็นภาษาที่สามารถใช้งานร่วมกับภาษา HTML ได้ ในการเขียน (Code) โปรแกรม มีวิธีการเขียนได้หลายรูปแบบ มี สัญลักษณ์ที่บ่งบอกถึงขอบเขตของ PHP เพื่อที่จะแยกโค้ด PHP ออกจากโค้ด HTML โดยมีรูปแบบในการเขียนแทนด้วยสัญลักษณ์ต่าง ๆ ที่เรา สามารถนำมาใช้แยกโค้ด PHP ได้
โดยเปิดแท็ก ด้วย <?php หรือ <? คำสัง แล้ว ปิดแท็กด้วย ?>

เปิดด้วยแท็ก < ? และปิดด้วยแท็ก ? > ภายใต้แท็ก < ? ... ? > คือคำสั่งที่ เราเขียนขึ้น ตามหลักของภาษา PHP
ตัวอย่าง
<html>
<head>
<title>Test PHP By cmullline</title>
</head>
<body>
<?php
 echo "Hello Experience Online";

?>

</body>
</html>

วันอาทิตย์ที่ 4 กันยายน พ.ศ. 2554

โดเมนเนม (Domain Name )

Domain Name ( โดเมนเนม )คือ ชื่อเว็บไซต์ (www.yourdomain.com) ที่ท่านสามารถเป็นเจ้าของ ซึ่งจะต้องไม่ซ้ำกับคนอื่น เพื่อการเรียกหาเว็บไซต์ที่ต้องการ “ชื่อเว็บไซต์” คือ สิ่งแรกที่แสดง หรือ ประกาศความมีตัวตนบนอินเตอร์เน็ตให้คนทั่วไปได้รู้จัก สามารถมีได้ชื่อเดียวในโลกเท่านั้น เช่น http://www.google.com/ เมื่อผู้ใช้กรอกชื่อลงไปในช่อง Address ของ Browser ก็จะส่งชื่อไปร้องถามจากเครื่องแปลชื่อ โดเมน (Domain Name Server) และได้รับกลับมาเป็นไอพีแอดเดรส แล้วส่งคำร้องไปให้กับเครื่องปลายทางตามไอพีแอดเดรส และได้ข้อมูลกลับมาตามรูปแบบที่ร้องขอไป