Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Azure > PHP and MySQL Using Azure Web Sites > ตอนที่ 3 : การเขียน PHP เพื่อติดต่อกับฐานข้อมูล MySQL และการอ่านข้อมูลมาแสดง



Clound SSD Virtual Server

ตอนที่ 3 : การเขียน PHP เพื่อติดต่อกับฐานข้อมูล MySQL และการอ่านข้อมูลมาแสดง

ตอนที่ 3 : การเขียน PHP เพื่อติดต่อกับฐานข้อมูล MySQL และการอ่านข้อมูลมาแสดง บทความนี้จะเป็นการเขียน PHP บน Web Site ของ Windows Azure ในการที่จะอ่านข้อมูลจาก Database ของ MySQL ที่อยู่อีก Region หนึ่ง ว่ามีรูปแบบการเชื่อมต่อได้ด้วยวิธีไหน โดยจะยกตัวอย่างการเขียน PHP เพื่อเชื่อมต่อแบบง่าย ๆ และการอ่านข้อมูลที่อยู่ใน Table ของ MySQL มาแสดงในหน้าจอ Web Browser

ในพื้นฐานของ PHP กับ MySQL เราจะเคยเขียนให้มันทำการเชื่อมต่อกับเครื่องเดียวกัน โดยเราจะคุ้น ๆ กับ Host คำว่า localhost หรือ 127.0.0.1 เช่น

<?php
$host = "localhost";
$username = "root";
$password = "root";
mysql_connect($host,$username,$password);
?>

โดยเราจะคุ้นกับการเชื่อมต่อแบบนี้ดี และ MySQL บน Windows Azure เราก็ใช้รูปแบบเหมือนกัน เพียงแค่เปลี่ยน Host และ User / Password เท่านั้น เช่น

Connection String
Database=thaicreateDB;Data Source=us-cdbr-azure-west-b.cleardb.com;User Id=bf8b0efb6c1b5b;Password=325bfa78


เราจะเขียนใหม่เป็น

<?php
$host = "us-cdbr-azure-west-b.cleardb.com";
$username = "bf8b0efb6c1b5b";
$password = "325bfa78";
mysql_connect($host,$username,$password);
?>









เรามาดูตัวอย่างการใช้งานจริง

Example 1 การเขียน PHP เชื่อมต่อกับ MySQL Database บน Windows Azure

connect.php
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<?php
$host = "us-cdbr-azure-west-b.cleardb.com";
$username = "bf8b0efb6c1b5b";
$password = "325bfa78";
$objConnect = mysql_connect($host,$username,$password);

if($objConnect)
{
	echo "MySQL Connected";
}
else
{
	echo "MySQL Connect Failed : Error : ".mysql_error();
}

mysql_close($objConnect);
?>
</body>
</html>

ไฟล์ PHP สำหรับเชื่อมต่อกับ MySQL บน Windows Azure

Windows Azure Web Site PHP Connect MySQL

ให้ Upload ไฟล์ไปยัง Web Site

Windows Azure Web Site PHP Connect MySQL

ทดสอบเรียกดูผลลัพธ์ ก็จะแสดงข้อความว่า MySQL Connected เมื่อมีการเชื่อมต่อสำเร็จ


Example 2 การเขียน PHP เชื่อมต่อกับ MySQL Database บน Windows Azure และการอ่านข้อมูลจากตารางมาแสดง

getData.php
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<?php
$host = "us-cdbr-azure-west-b.cleardb.com";
$username = "bf8b0efb6c1b5b";
$password = "325bfa78";
$objConnect = mysql_connect($host,$username,$password);

$objDB = mysql_select_db("thaicreatedb");

$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL) or die (mysql_error());

?>
<table width="600" border="1">
  <tr>
    <th width="91"> <div align="center">CustomerID </div></th>
    <th width="98"> <div align="center">Name </div></th>
    <th width="198"> <div align="center">Email </div></th>
    <th width="97"> <div align="center">CountryCode </div></th>
    <th width="59"> <div align="center">Budget </div></th>
    <th width="71"> <div align="center">Used </div></th>
  </tr>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
?>
  <tr>
    <td><div align="center"><?php echo $objResult["CustomerID"];?></div></td>
    <td><?php echo $objResult["Name"];?></td>
    <td><?php echo $objResult["Email"];?></td>
    <td><div align="center"><?php echo $objResult["CountryCode"];?></div></td>
    <td align="right"><?php echo $objResult["Budget"];?></td>
    <td align="right"><?php echo $objResult["Used"];?></td>
  </tr>
<?php
}
?>
</table>
<?php
mysql_close($objConnect);
?>
</body>
</html>


Windows Azure Web Site PHP Connect MySQL

ให้ Upload ไฟล์ไปยัง Web Site

Windows Azure Web Site PHP Connect MySQL

ทดสอบเรียกดูผลลัพธ์ ซึ่งเป็นการแสดงข้อมูลต่าง ๆ จากตารางที่ชื่อว่า customer ที่อยู่บน MySQL Database ของ Windows Azure








บทความถัดไปที่แนะนำให้อ่าน
อ่านเพิ่มเติม

บทความ PHP กับ MySQL ที่แนะนำให้อ่าน

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-02-25 14:15:08 / 2017-03-24 09:56:58
  Download : No files
 Sponsored Links / Related

 
ตอนที่ 1 : การสร้าง Web Site และ MySQL Database บน Windows Azure
Rating :

 
ตอนที่ 2 : การจัดการ MySQL บน Windows Azure ด้วย MySQL Workbench
Rating :

 
ตอนที่ 4 : การเขียน PHP กับ MySQL บน Azure Web Site บันทึกแก้ไขข้อมูล
Rating :

 
ตอนที่ 5 : การติดตั้ง Script ฟรี Open Source เช่น Joomla ลง Azure Web Site
Rating :

 
ตอนที่ 6 : การสร้าง Web Service แบบง่าย ๆ บน Azure Web Site
Rating :

 
ตอนที่ 7 : การใช้งาน htaccess กับ mod_rewrite บน (IIS) Azure Web Site
Rating :

 
ตอนที่ 8 : การสร้าง Service Add-on จาก Store เช่น ClearDB MySQL DB บน Azure
Rating :

 
ตอนที่ 9 : แหล่งความรู้ Document เกี่ยวกับ PHP Website และ Windows Azure
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่