Posts tagged: sql

TRUNCATE TABLES ALL

By สุดเดช, 19 กุมภาพันธ์ 2009 19:25

เมื่อผู้พัฒนา เรื่องเข้าใจ ในการทำงานของ คลาส ดาต้าเบส แล้ว
ต่อจากนี้ผมจะเอาตัวที่จัดการ การทำงาน ของ MySQL ตัวหนึ่งมาให้รู้กัน
นั้นคือ คำสั่ง TRUNCATE นั้นเองการทำงานของคำสั่งนี่คือการทำให้ Table ที่ทุกสั่งนั้น จะเป็น EMPTY ทั้งหมด
ในโปรแกรม phpmyadmin นั้นมีอยู่แล้ว แต่ว่า ผมอยากเขียนขึ้นมาใหม่อ่ะ ขี้เกียจไปเข้ามันช้า

มาดูการเขียนกันคับ
รูปแบบคำสั่งของมันจะเป็นแบบนี้คับ และ เพิ่มเติมให้อีกอันน่ะคับ นั้นก็คือตัว SHOW TABLES;

TRUNCATE TABLE ชื่อเทเบิ้ล;

ex TRUNCATE TABLE tbl_model;

งั้นมาดูโค้ดที่ผมเขียนกันเลยน่ะคับ

ขั้นตอนแรกก็ include ไฟล์ที่ชื่อว่า class.mysql.php เข้ามาก่อนน่ะคับ
แ้ล้วไปตั้ง ค่า config ที่ตัว constant กันก่อนคับ ลอง ๆ หาดู จะได้คล่อง
ถ้าติดตรงไหนก็ถามผมได้คับยินดีตอบคับ

<?
include("class.mysql.php");

$db = new db;
$db->connectdb(DB_NAME,DB_USERNAME,DB_PASSWORD);

$tables=$_GET["tables"];
if($tables){
if($tables=="all"){

$sql=$db->select_query("show tables;");
while(list($query_tables)=$db->fetch($sql)){
$db->select_query("TRUNCATE TABLE $query_tables;");
}
echo"TRUNCATE TABLE ALL";
header("refresh: 3; url=truncate_table.php");
}else{
$check=$db->select_query("TRUNCATE TABLE $tables;");
if($check){
echo"System :: <br />";
echo"<u>TRUNCATE TABLE $tables;</u><br />";
echo" Finish<br />";
header("refresh: 3; url=truncate_table.php");
}else{
echo"Have not your select table";
header("refresh: 3; url=truncate_table.php");
}
}
}else{
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TRUNCATE TABLE </title>
</head>

<body>
<h1>Select Truncate Table</h1>
<ul>
<?
$sql=$db->select_query("show tables;");
while(list($show_tables)=$db->fetch($sql)){
?>
<li>
<a href="?tables=<?=$show_tables;?>" onclick="javascript:return confirm('TRUNCATE TABLE <?=$show_tables;?>;');">
<?=$show_tables;?>
</a>
</li>
<?
}
?>
</ul>
<a href="?tables=all" onclick="javascript:return confirm('TRUNCATE TABLE ALL;');">
[TRUNCATE TABLES ALL]</a>
</body>
</html>
<?
}
?>

RESULT :

truncate_table

ใครมีอะไรที่เจ๋ง ๆ ก็แนะนำกันหน่อยน่ะคับ โลกใบนี้หาความรู้ได้ทุกวันจริง ๆ ฮ าๆ

Bookmark and Share

Enum Type

By สุดเดช, 7 มกราคม 2009 22:07

The ENUM Type

An ENUM is a string object with a value chosen from a list of allowed values that are enumerated explicitly in the column specification at table creation time.

For example, a column specified as ENUM('one', 'two', 'three') can have any of the values shown here. The index of each value is also shown:

enum1

An enumeration can have a maximum of 65,535 elements.

Here is an working example that ensures you that you will get the values inside a enum field in your mysql table.


function enum($table, $field)
{
$result = @mysql_query("show columns from {$table} like \"$field\"");
$result  = @mysql_fetch_assoc($result);
if($result["Type"])
{
preg_match("/(enum\((.*?)\))/", $result["Type"], $enumArray);
$getEnumSet = explode("'", $enumArray["2"]);
$getEnumSet = preg_replace("/,/", "", $getEnumSet);
$enumFields = array();
foreach($getEnumSet as $enumFieldValue)
{
if($enumFieldValue)
{
$enumFields[] = $enumFieldValue;
}
}
return $enumFields;
}
return "Unable to get enum FIELD {$field} from table {$table}";
}

// Returns an array set of all values of a enum field.
print_r(enum("MY_TABLE_NAME","MY_FIELD_NAME"));

link : http://dev.mysql.com/doc/refman/5.0/en/enum.html

Bookmark and Share

Panorama theme by Themocracy