Location: PHPKode > projects > AJAX MySQL Admin > ajaxmysqladmin/www/api/objects.php
<?php
    require_once ('inc/constants.inc');
    require_once ('inc/generic_start.inc');
    $shown = false;

    if ($dbname !== null) {
        if ($_GET['action'] == 'addtable' && isset ($_GET['name'])) {
            $name = $_GET['name'];

            $found = 0;

            $query = "SHOW TABLES FROM $dbname";
            $conn = mysql_connect ($dbhost . ":" . $dbport, $dbuser, $dbpass);
            $result = mysql_query ($query, $conn);
            if ($result !== FALSE && mysql_num_rows ($result) > 0) {
                $count = 0;
                while ($row = mysql_fetch_row($result)) {
                    if ($name == $row [0]) {
                        $found = 1;
                    }
                }
            }

            if ($found == 1) {
                echo "{response:100}";
            } else {
                echo "{";
                mysql_select_db ($dbname, $conn);
                $query = "CREATE TABLE `" . $name . "` (id int);";
                if (mysql_query ($query, $conn) === FALSE) {
                    echo "mysqlerror:'" . str_replace ('\'', '\\\'', mysql_error()) . "',";
                    echo "response:0";
                } else {
                    echo "response:1";
                }
                echo "}";
            }

            $shown = true;
        } else if ($_GET['action'] == 'adddb' && isset ($_GET['name'])) {
            $name = $_GET['name'];

            $query = "SHOW DATABASES";
            $conn = mysql_connect ($dbhost . ":" . $dbport, $dbuser, $dbpass);
            $result = mysql_query ($query, $conn);
            if ($result !== FALSE && mysql_num_rows ($result) > 0) {
                $count = 0;
                $found = 0;
                while ($row = mysql_fetch_row($result)) {
                    if ($name == $row [0]) {
                        $found = 1;
                    }
                }
                if ($found == 1) {
                    echo "{response:10}";
                } else {
                    $query = "CREATE DATABASE `" . $name . "`;";
                    mysql_query ($query, $conn);
                    echo "{response:1}";
                }
                $shown = true;
            }
        } else if ($_GET['action'] == 'addcol' && isset ($_GET['tname']) && isset ($_GET['label']) && isset ($_GET['type'])) {
            $tname = $_GET['tname'];
            $label = $_GET['label'];
            $type = $_GET['type'];
            $size = $_GET['size'];

            $query = "ALTER TABLE `" . $tname . "` ADD COLUMN `" . $label . "` " . $type . ($size != '' ? "(" . $size . ")" : "") . ";";
            $conn = mysql_connect ($dbhost . ":" . $dbport, $dbuser, $dbpass);
            mysql_select_db ($dbname, $conn);
            echo "{";
            if (mysql_query ($query, $conn) === FALSE) {
                echo "mysqlerror:'" . str_replace ('\'', '\\\'', mysql_error()) . "',";
                echo "response:0";
            } else {
                echo "response:1";
            }
            echo "}";
            $shown = true;
        } else if ($_GET['action'] == 'removecol' && isset ($_GET['tname']) && isset ($_GET['label'])) {
            $tname = $_GET['tname'];
            $label = $_GET['label'];

            $query = "ALTER TABLE `" . $tname . "` DROP COLUMN `" . $label . "`;";
            $conn = mysql_connect ($dbhost . ":" . $dbport, $dbuser, $dbpass);
            mysql_select_db ($dbname, $conn);
            echo "{";
            if (mysql_query ($query, $conn) === FALSE) {
                echo "mysqlerror:'" . str_replace ('\'', '\\\'', mysql_error()) . "',";
                echo "response:0";
            } else {
                echo "response:1";
            }
            echo "}";
            $shown = true;
        } else if ($_GET['action'] == 'removedb' && isset ($_GET['name'])) {
            $name = $_GET['name'];

            $query = "SHOW DATABASES";
            $conn = mysql_connect ($dbhost . ":" . $dbport, $dbuser, $dbpass);
            $result = mysql_query ($query, $conn);
            if ($result !== FALSE && mysql_num_rows ($result) > 0) {
                $count = 0;
                $found = 0;
                while ($row = mysql_fetch_row($result)) {
                    if ($name == $row [0]) {
                        $found = 1;
                    }
                }
                if ($found == 1) {
                    $query = "DROP DATABASE `" . $name . "`";
                    echo "{";
                    if (mysql_query ($query, $conn) === FALSE) {
                        echo "mysqlerror:'" . str_replace ('\'', '\\\'', mysql_error()) . "',";
                        echo "response:0";
                    } else {
                        echo "response:1";
                    }
                    echo "}";
                } else {
                    echo "{response:11}";
                }
                $shown = true;
            }
        } else if ($_GET['action'] == 'removetable' && isset ($_GET['name'])) {
            $name = $_GET['name'];

            $found = 0;

            $query = "SHOW TABLES FROM $dbname";
            $conn = mysql_connect ($dbhost . ":" . $dbport, $dbuser, $dbpass);
            $result = mysql_query ($query, $conn);
            if ($result !== FALSE && mysql_num_rows ($result) > 0) {
                $count = 0;
                while ($row = mysql_fetch_row($result)) {
                    if ($name == $row [0]) {
                        $found = 1;
                    }
                }
            }

            if ($found == 1) {
                echo "{";
                mysql_select_db ($dbname, $conn);
                $query = "DROP TABLE `" . $name . "`;";
                if (mysql_query ($query, $conn) === FALSE) {
                    echo "mysqlerror:'" . str_replace ('\'', '\\\'', mysql_error()) . "',";
                    echo "response:0";
                } else {
                    echo "response:1";
                }
                echo "}";
            } else {
                echo "{response:101}";
            }
            $shown = true;
        } else {
            $query = "SHOW TABLES FROM $dbname";
            $conn = mysql_connect ($dbhost . ":" . $dbport, $dbuser, $dbpass);
            $result = mysql_query ($query, $conn);
            if ($result !== FALSE && mysql_num_rows ($result) > 0) {
                ?>
                {
                    tables: [
                    <?php
                        $count = 0;
                        while ($row = mysql_fetch_row($result)) {
                            if ($count > 0) echo "," . NL;
                            echo "'" . $row[0] . "'";
                            $count ++;
                        }
                        mysql_free_result ($result);
                    ?>
                    ]
                }
                <?php
            } else {
                ?>
                {response:0,mysqlerror:<?php echo mysql_error (); ?>}
                <?php
            }
            $shown = true;
            mysql_close ($conn);
        }
    }

    if (!$shown) {
        echo "{response:0}";
    }

    require_once ('inc/generic_finish.inc');
?>
Return current item: AJAX MySQL Admin