Advertisement
thexiv

MySQLi Wrappers

Nov 26th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.37 KB | None | 0 0
  1. <?php
  2. if (!isset($_SESSION))
  3.     session_start();
  4.  
  5. function cols_from_mysql($conn, $table = "") {
  6.     if ($table == "")
  7.         return "Usage: cols_from_mysql(mysqli_connxn, table1);";
  8.     $sql = 'SHOW COLUMNS FROM ' . $table;
  9.     $res = $conn->query($sql);
  10.     unset($_SESSION['columns']);
  11.     while($row = $res->fetch_assoc()) {
  12.         $r = 0;
  13.         foreach ($_SESSION['columns'][$table] as $cols)
  14.             if ($cols == $row['Field'])
  15.                 $r = 1;
  16.         if ($r == 0)
  17.             $_SESSION['columns'][$table][] = $row['Field'];
  18.     }
  19.     return 1;
  20. }
  21.  
  22. function update($table = "", $c = null, $cols = null, $arr = null, $error = 0) {
  23.     if ($cols == null && $arr == null && $error == 0)
  24.         return 'Usage: $query = update("table1, table2", "column = new_value", null, null, 1) to update all rows';
  25.     else if ($table == "" || $c == null || !is_array($cols) || $cols == null || sizeof($arr) != sizeof($cols)-1)
  26.         return 'Usage: $query = update("table1, table2", "column = new_value", array("a >= b", "c < d"), array("AND","OR"))';
  27.     $query = "UPDATE $table SET " . $c;
  28.     if ($error == 1) {
  29.         $query .= " WHERE 1";
  30.         return $query;
  31.     }
  32.     if (sizeof($arr) > 0 && sizeof($arr) == $sizeof($cols) - 1) {
  33.         $query .= " WHERE ";
  34.         for ($i = 0 ; $i < sizeof($cols) ; $i++)
  35.             if ($i < sizeof($cols) - 1)
  36.                 $query .= $cols[$i] . " " . $arr[$i] . " ";
  37.             else
  38.                 $query .= $cols[$i];
  39.     }
  40.     return $query;
  41.  
  42. }
  43. function delete($table = "", $cols = null, $arr = null, $error = 0) {
  44.     if ($cols == null && $arr == null && $error == 0)
  45.         return 'Usage: $query = delete("table1, table2", null, null, 1) to erase all rows';
  46.     else if ($table == "" || !is_array($cols) || $cols = null || sizeof($arr) != sizeof($cols)-1)
  47.         return 'Usage: $query = delete("table1, table2", array("a >= b", "c < d"), array("AND","OR"))';
  48.     $query = "DELETE FROM $table";
  49.     if ($error == 1) {
  50.         $query .= " WHERE 1";
  51.         return $query;
  52.     }
  53.     if (sizeof($arr) > 0 && sizeof($arr) == $sizeof($cols) - 1) {
  54.         $query .= " WHERE ";
  55.         for ($i = 0 ; $i < sizeof($cols) ; $i++)
  56.             if ($i < sizeof($cols) - 1)
  57.                 $query .= $cols[$i] . " " . $arr[$i] . " ";
  58.             else
  59.                 $query .= $cols[$i];
  60.     }
  61.     return $query;
  62. }
  63.  
  64. function select($table = "", $cols = array(), $arr = null) {
  65.     if ($table == "" || !is_array($cols) || $cols == null || sizeof($arr) != sizeof($cols)-1)
  66.         return 'Usage: $query = select("table1, table2", "array(columns) (array() for *)", [WHERE=array("a >= b", "c < d")], [BINARY_DECISIONS=array("AND","OR")])';
  67.     $query = "SELECT ";
  68.     if (sizeof($cols) == 0)
  69.         $query .= "*";
  70.     else {
  71.         foreach ($cols as $c) {
  72.             query .= $c . ",";
  73.         }
  74.     }
  75.     $p = "";
  76.     for ($i = 0 ; $i < strlen($query)-1 ; $i++)
  77.         $p .= $query[$i];
  78.     if ($p != "")
  79.         $query = $p;
  80.     $query .=  " FROM " . $table;
  81.     if (sizeof($arr) > 0 && sizeof($arr) == $sizeof($cols) - 1) {
  82.         $query .= " WHERE ";
  83.         for ($i = 0 ; $i < sizeof($cols) ; $i++)
  84.             if ($i < sizeof($cols) - 1)
  85.                 $query .= $cols[$i] . " " . $arr[$i] . " ";
  86.             else
  87.                 $query .= $cols[$i];
  88.     }
  89.     return $query;
  90. }
  91.  
  92. function insert($table) {
  93.     $query = "INSERT INTO " . $table . " (";
  94.  
  95.     foreach($_SESSION['columns'][$table] as $col)
  96.         $query .=  $col . ",";
  97.     $p = "";
  98.     for ($i = 0 ; $i < strlen($query)-1 ; $i++)
  99.         $p .= $query[$i];
  100.     $query = $p;
  101.     $query .= ") VALUES (";
  102.     foreach($_SESSION['columns'][$table] as $col)
  103.         $query .=  form_column_value($col) . ",";
  104.     $p = "";
  105.     for ($i = 0 ; $i < strlen($query)-1 ; $i++)
  106.         $p .= $query[$i];
  107.     $query = $p;
  108.     $query .= ")";
  109.     return $query;
  110. }
  111.  
  112. function set_columns($array_cols, $table) {
  113.     if ($table == "")
  114.         throw("Error: parameter 2: tablename needed");
  115.     if (!is_array($array_cols))
  116.         throw("Error: parameter 1: array needed");
  117.     foreach ($array_cols as $var) {
  118.         $r = 0;
  119.         if (is_array($var) || is_object($var))
  120.             continue;
  121.         foreach ($_SESSION['columns'][$table] as $cols)
  122.             if ($cols == $var)
  123.                 $r = 1;
  124.         if ($r == 0)
  125.             $_SESSION['columns'][$table][] = $var;
  126.     }
  127. }
  128.  
  129. function form_column_value($var) {
  130.     if (isset($_POST[$var]))
  131.         return $_POST[$var];
  132.     else if (isset($_GET[$var]))
  133.         return $_GET[$var];
  134.     return "null";
  135. }
  136.  
  137. $conn = new mysqli($hostname, $username, $password, $dbname);
  138.  
  139. if ($conn->connect_error) {
  140.     die("Connection failed: " . $conn->connect_error);
  141. }
  142.  
  143. $arr = array('check','t','mmm','t');
  144.  
  145. set_columns($arr);
  146.  
  147. echo insert('table');
  148.  
  149. echo json_encode($_SESSION['columns']);
  150. foreach ($_SESSION['columns'] as $var) {
  151.     echo form_column_value($var);
  152. }
  153.  
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement