<?php
/*
Updated : 2561-11-15
ช่วยบริหารจัดการ mysql database ในกรณีไม่มี phpmyadmin
รับคำสั่ง sql ไปประมวลผล ทั้ง show tables, update, select
ทดสอบบน xampp พบว่าใช้งานได้ตามวัตถุประสงค์
ข้อเสนอแนะ
- ควรปรับให้ code ทำงานเมื่อกรอกรหัสผ่านถูก และเก็บรหัสใน session เพื่อทำงาน
#######################################
*/
session_start();
$host = "localhost";
$db = "mysql";
$user = "root";
$password = "";
$my_password = "test";
check_password();
$connect = new mysqli($host,$user,$password,$db);
$result = $connect->query("show tables");
show_tables();
if(isset($_POST["select"])) { do_select($_POST["code_select"]); }
if(isset($_POST["update"])) { do_update($_POST["code_update"]); }
if(isset($_GET["act"]) && $_GET["act"]=="fields" ) { do_show_fields($_GET["tb"]); }
?>
<form action="" method="post" name="select"><textarea name="code_select" rows="5" cols="80">
select * from user;
select * from db;
/* select * from mytest
show tables
show databases
*/
</textarea><br/><input type="submit" value="select" name="select">
</form>
<form action="" method="post" name="update"><textarea name="code_update" rows="10" cols="80">
create table mytest (t1 int primary key auto_increment,t2 int, t3 varchar(20),t4 char(20));
insert into mytest values("",234,"tom","boy");
insert into mytest values("",456,"jojo","girl");
/*
delete from mytest where t1=1
update mytest set t2=789,t3="romeo" where a1=1
-- drop table mytest
*/
</textarea><br/><input type="submit" value="update" name="update">
</form>
<?php
function check_password(){
global $my_password;
if(isset($_GET["act"]) && $_GET["act"] =="signout") {
$_SESSION["my_password"] = "";
echo "<meta http-equiv=\"refresh\" content=\"0; url=?\">";
}
if(isset($_POST["p"]) && $_POST["p"] == $my_password) $_SESSION["my_password"] = $my_password;
if(!isset($_SESSION["my_password"]) || $_SESSION["my_password"] != $my_password){
echo "<form action='' method='post'><input name=p><input type=submit value=signin></form>";
exit;
}
}
function do_count_rec($o){
global $connect;
$do_result = $connect->query("select count(*) as cnt from $o");
if($obj = mysqli_fetch_array($do_result)) return ": $obj[0] records"; else return 0;
}
function do_count_field($o){
global $connect;
$do_result = $connect->query("select * from $o");
$numField = mysqli_num_fields($do_result);
return ": $numField fields";
}
function do_show_fields($tb){
global $connect;
$do_result = $connect->query("select * from $tb");
$numField = mysqli_num_fields($do_result);
echo "<ol>Table : $tb";
while ($fieldinfo=mysqli_fetch_field($do_result)) {
echo "<li>".$fieldinfo->name . "</li>";
}
echo "</ol>";
}
function do_select($sql){
global $connect;
$ar = explode(";", $sql);
if(count($ar) > 2){
for($i=0;$i<count($ar) - 1;$i++){
if(strlen($ar[$i]) > 5) do_select($ar[$i]);
}
return null;
}
$do_result = $connect->query($ar[0]); // ฟังก์ชันเวียนบังเกิด (Recursive Function)
echo "<b>".$ar[0]."</b><br/>";
while ($obj = $do_result->fetch_object()) {
foreach ($obj as $my_o) echo "$my_o ";
echo "<br/>";
}
}
function do_update($sql){
global $connect;
$ar = explode(";", $sql);
if(count($ar) > 2){
for($i=0;$i<count($ar) - 1;$i++){
if(strlen($ar[$i]) > 5) do_update($ar[$i]);
}
return null;
}
$do_result = $connect->query($ar[0]); // ฟังก์ชันเวียนบังเกิด (Recursive Function)
echo $ar[0] ." : ";
if ($do_result === TRUE) echo "complete"; else echo "fail";
echo "<meta http-equiv=\"refresh\" content=\"5; url=?\">";
}
function show_tables(){
global $connect,$result;
echo "<a href='?".time()."'>Home</a> : Total Tables = ".$result->num_rows
." : <a href='?act=signout'>Sign Out</a><ol>";
while ($object = $result->fetch_object()) {
echo "<li>";
foreach ($object as $o) {
echo "$o " . do_count_rec($o). do_count_field($o)
. " [<a href=?act=fields&tb=$o>show fields</a>]";
}
echo "</li>";
}
echo "</ol>";
}
?>
จำนวน : 123 บรรทัด