PHP : ติดต่อฐานข้อมูล MySQL ด้วย php

<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}

$sql = "SELECT * FROM Person";
mysql_query($sql,$con);

// some code

mysql_close($con);
?>
<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}

$sql = "CREATE DATABASE my_db";
if (mysql_query($sql,$con))
{
	echo "Database my_db created";
}
else
{
	echo "Error creating database: " . mysql_error();
}
?>

ที่มา : www.w3schools.com/

กรณีที่ต้องการใช้ภาษาไทยให้กำหนด Collation เป็น utf8_unicode_ci ดังนี้

<?php
	$dbhost = 'localhost';
	$dbname = 'dbxxx';
	$dbuser = 'userxxx';
	$dbpass = 'passxxx';

	$conn = mysql_connect($dbhost, $dbuser, $dbpass) 
             or die ('Error connecting to mysql');
	mysql_select_db($dbname);
	mysql_query("SET NAMES UTF8");
	$sql = "SELECT * FROM sometable";

	if ($objQuery = mysql_query($sql, $conn))
	{
		echo "OK";		
	}
	else
	{
		echo "Error " . mysql_error();
	}
	mysql_close($conn);
?>