PHP MySQL Connect to Database สำหรับการเขียน PHP กับ MySQL นั้นถือเป็นปัจจัยหลักและได้รับความนิยมมากที่สุดในบรรดา Database ทั้งหลายครับ เพราะนอกจากฟรีทั้ง 2 ตัวแล้ว function ต่าง ๆ PHP ได้ออกแบบมาให้ใช้กับ MySQL Database โดยเฉพาะครับ จุดเด่นของ PHP และ MySQL คือ MySQL สามารถทงานได้อย่างรวดเร็ว และค่อนข้างจะเก็บข้อมูลได้หลาย Record ครับ
mysql_error()
Fatal error: Call to undefined function mysql_connect()
in C:\AppServ\www\myphp\phpMySQLConnect.php on line 7
in C:\AppServ\www\myphp\phpMySQLConnect.php on line 7
ถ้าเกิดข้อผิดพลาดตามที่ผมยกตัวอย่างให้ให้ทำการดาวน์โหลดตัว extension ชื่อ php_mysql.dll
php_mysql.dll
ไปไว้ในโฟเดอร์ extension ของ php เช่น C:\AppServ\php5\ext หรือสามารถตรวจสอบ Path ได้ที่ php.ini
Start -> Run -> php.ini
extension_dir = "C:/AppServ\php5\ext"
.
.
.
.
.
extension=php_mysql.dll
.
.
.
.
.
extension=php_mysql.dll
และก็ Restart Apache หรือ Web Server ด้วยน่ะครับ เสร็จแล้วเป็นอันใช้ได้
Syntax
$objConnect = mysql_connect($host,$user,$pass);
$host = server,ip,computer-name
$user = user
$pass = password
$host = server,ip,computer-name
$user = user
$pass = password

phpMySQLListRecord.php
Screenshot

PHP MySQL Search Record ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อค้นหาข้อมูลจาก Table
Screenshot

PHP MySQL List Record Paging/Pagination ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อดึงข้อมูลจาก Table มาแสดงและมีการแบ่งการแสดงผลเป็นหน้า
Screenshot

PHP MySQL Multiple Column ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อแสดงข้อมูลจาก Table โดยมีการจัดรูปแบบการแบ่ง Column (คอลัมบ์) ในการแสดงผลด้วย
Screenshot

PHP MySQL Add/Insert Record ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อเพิ่มข้อมูลลงใน Table
Screenshot

phpMySQLAddSave.php
01.<html>02.<head>03.<title>ThaiCreate.Com PHP & MySQL Tutorial</title>04.</head>05.<body>06.<?php07.$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");08.$objDB = mysql_select_db("mydatabase");09.$strSQL = "INSERT INTO customer ";10.$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";11.$strSQL .="VALUES ";12.$strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";13.$strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";14.$objQuery = mysql_query($strSQL);15.if($objQuery)16.{17.echo "Save Done.";18.}19.else20.{21.echo "Error Save [".$strSQL."]";22.}23.mysql_close($objConnect);24.?>25.</body>26.</html>Screenshot

PHP MySQL Check Already Exists Add/Insert Record ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL ตรวจสอบข้อมูลก่อนเพิ่มข้อมูลลงใน Table
ตัวอย่าง
phpMySQLCheckExistRecordAddForm.php

phpMySQLCheckExistRecordAddSave.php
Screenshot

PHP MySQL Edit/Update Data Record ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อทำการเรียกข้อมูลจากMySQL มาแก้ไข
Screenshot

phpMySQLEditRecordSave.php
Screenshot

PHP MySQL Delete Record ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อลบข้อมูลใน Table พร้อมทั้งการยืนยันการลบข้อมูล

PHP MySQL Add/Insert/Edit/Delete to MySQL On Same Form เป็นตัวอย่างการเขียน PHP & MySQL ให้สามารถ เพิ่ม/แก้ไข/ลบ ข้อมูลใน Form เดียวกัน คล้าย ๆ กับ DataGrid หรือ GridView

phpMySQLCheckExistRecordAddForm.php

phpMySQLCheckExistRecordAddSave.php
01.<html>02.<head>03.<title>ThaiCreate.Com PHP & MySQL Tutorial</title>04.</head>05.<body>06.<?php07.$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");08.$objDB = mysql_select_db("mydatabase");09.$strSQL = "SELECT * FROM customer WHERE CustomerID = '".$_POST["txtCustomerID"]."' ";10.$objQuery = mysql_query($strSQL);11.$objResult = mysql_fetch_array($objQuery);12.if($objResult)13.{14.echo "CustomerID already exist.";15.}16.else17.{18. 19.$strSQL = "";20.$strSQL = "INSERT INTO customer ";21.$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";22.$strSQL .="VALUES ";23.$strSQL .="('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ";24.$strSQL .=",'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."') ";25.$objQuery = mysql_query($strSQL);26.if($objQuery)27.{28.echo "Save Done.";29.}30.else31.{32.echo "Error Save [".$strSQL."]";33.}35.mysql_close($objConnect);36.?>37.</body>38.</html>Screenshot

PHP MySQL Edit/Update Data Record ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อทำการเรียกข้อมูลจากMySQL มาแก้ไข
Screenshot

phpMySQLEditRecordSave.php
01.<html>02.<head>03.<title>ThaiCreate.Com PHP & MySQL Tutorial</title>04.</head>05.<body>06.<?php07.$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");08.$objDB = mysql_select_db("mydatabase");09.$strSQL = "UPDATE customer SET ";10.$strSQL .="CustomerID = '".$_POST["txtCustomerID"]."' ";11.$strSQL .=",Name = '".$_POST["txtName"]."' ";12.$strSQL .=",Email = '".$_POST["txtEmail"]."' ";13.$strSQL .=",CountryCode = '".$_POST["txtCountryCode"]."' ";14.$strSQL .=",Budget = '".$_POST["txtBudget"]."' ";15.$strSQL .=",Used = '".$_POST["txtUsed"]."' ";16.$strSQL .="WHERE CustomerID = '".$_GET["CusID"]."' ";17.$objQuery = mysql_query($strSQL);18.if($objQuery)19.{20.echo "Save Done.";21.}22.else23.{24.echo "Error Save [".$strSQL."]";25.}26.mysql_close($objConnect);27.?>28.</body>29.</html>Screenshot

PHP MySQL Delete Record ตัวอย่างนี้จะเป็นการเขียนโปรแกรม PHP กับ MySQL เพื่อลบข้อมูลใน Table พร้อมทั้งการยืนยันการลบข้อมูล

PHP MySQL Add/Insert/Edit/Delete to MySQL On Same Form เป็นตัวอย่างการเขียน PHP & MySQL ให้สามารถ เพิ่ม/แก้ไข/ลบ ข้อมูลใน Form เดียวกัน คล้าย ๆ กับ DataGrid หรือ GridView

ไม่มีความคิดเห็น:
แสดงความคิดเห็น