Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!isset($_SESSION))
- session_start();
- function cols_from_mysql($conn, $table = "") {
- if ($table == "")
- return "Usage: cols_from_mysql(mysqli_connxn, table1);";
- $sql = 'SHOW COLUMNS FROM ' . $table;
- $res = $conn->query($sql);
- unset($_SESSION['columns']);
- while($row = $res->fetch_assoc()) {
- $r = 0;
- foreach ($_SESSION['columns'][$table] as $cols)
- if ($cols == $row['Field'])
- $r = 1;
- if ($r == 0)
- $_SESSION['columns'][$table][] = $row['Field'];
- }
- return 1;
- }
- function update($table = "", $c = null, $cols = null, $arr = null, $error = 0) {
- if ($cols == null && $arr == null && $error == 0)
- return 'Usage: $query = update("table1, table2", "column = new_value", null, null, 1) to update all rows';
- else if ($table == "" || $c == null || !is_array($cols) || $cols == null || sizeof($arr) != sizeof($cols)-1)
- return 'Usage: $query = update("table1, table2", "column = new_value", array("a >= b", "c < d"), array("AND","OR"))';
- $query = "UPDATE $table SET " . $c;
- if ($error == 1) {
- $query .= " WHERE 1";
- return $query;
- }
- if (sizeof($arr) > 0 && sizeof($arr) == $sizeof($cols) - 1) {
- $query .= " WHERE ";
- for ($i = 0 ; $i < sizeof($cols) ; $i++)
- if ($i < sizeof($cols) - 1)
- $query .= $cols[$i] . " " . $arr[$i] . " ";
- else
- $query .= $cols[$i];
- }
- return $query;
- }
- function delete($table = "", $cols = null, $arr = null, $error = 0) {
- if ($cols == null && $arr == null && $error == 0)
- return 'Usage: $query = delete("table1, table2", null, null, 1) to erase all rows';
- else if ($table == "" || !is_array($cols) || $cols = null || sizeof($arr) != sizeof($cols)-1)
- return 'Usage: $query = delete("table1, table2", array("a >= b", "c < d"), array("AND","OR"))';
- $query = "DELETE FROM $table";
- if ($error == 1) {
- $query .= " WHERE 1";
- return $query;
- }
- if (sizeof($arr) > 0 && sizeof($arr) == $sizeof($cols) - 1) {
- $query .= " WHERE ";
- for ($i = 0 ; $i < sizeof($cols) ; $i++)
- if ($i < sizeof($cols) - 1)
- $query .= $cols[$i] . " " . $arr[$i] . " ";
- else
- $query .= $cols[$i];
- }
- return $query;
- }
- function select($table = "", $cols = array(), $arr = null) {
- if ($table == "" || !is_array($cols) || $cols == null || sizeof($arr) != sizeof($cols)-1)
- return 'Usage: $query = select("table1, table2", "array(columns) (array() for *)", [WHERE=array("a >= b", "c < d")], [BINARY_DECISIONS=array("AND","OR")])';
- $query = "SELECT ";
- if (sizeof($cols) == 0)
- $query .= "*";
- else {
- foreach ($cols as $c) {
- query .= $c . ",";
- }
- }
- $p = "";
- for ($i = 0 ; $i < strlen($query)-1 ; $i++)
- $p .= $query[$i];
- if ($p != "")
- $query = $p;
- $query .= " FROM " . $table;
- if (sizeof($arr) > 0 && sizeof($arr) == $sizeof($cols) - 1) {
- $query .= " WHERE ";
- for ($i = 0 ; $i < sizeof($cols) ; $i++)
- if ($i < sizeof($cols) - 1)
- $query .= $cols[$i] . " " . $arr[$i] . " ";
- else
- $query .= $cols[$i];
- }
- return $query;
- }
- function insert($table) {
- $query = "INSERT INTO " . $table . " (";
- foreach($_SESSION['columns'][$table] as $col)
- $query .= $col . ",";
- $p = "";
- for ($i = 0 ; $i < strlen($query)-1 ; $i++)
- $p .= $query[$i];
- $query = $p;
- $query .= ") VALUES (";
- foreach($_SESSION['columns'][$table] as $col)
- $query .= form_column_value($col) . ",";
- $p = "";
- for ($i = 0 ; $i < strlen($query)-1 ; $i++)
- $p .= $query[$i];
- $query = $p;
- $query .= ")";
- return $query;
- }
- function set_columns($array_cols, $table) {
- if ($table == "")
- throw("Error: parameter 2: tablename needed");
- if (!is_array($array_cols))
- throw("Error: parameter 1: array needed");
- foreach ($array_cols as $var) {
- $r = 0;
- if (is_array($var) || is_object($var))
- continue;
- foreach ($_SESSION['columns'][$table] as $cols)
- if ($cols == $var)
- $r = 1;
- if ($r == 0)
- $_SESSION['columns'][$table][] = $var;
- }
- }
- function form_column_value($var) {
- if (isset($_POST[$var]))
- return $_POST[$var];
- else if (isset($_GET[$var]))
- return $_GET[$var];
- return "null";
- }
- $conn = new mysqli($hostname, $username, $password, $dbname);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $arr = array('check','t','mmm','t');
- set_columns($arr);
- echo insert('table');
- echo json_encode($_SESSION['columns']);
- foreach ($_SESSION['columns'] as $var) {
- echo form_column_value($var);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement