darkmaterial

Simple SQL Connection function for PHP

Apr 29th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.11 KB | None | 0 0
  1. function sqlcon($sql){
  2. /*Erklärung des Return Arrays sqlcon
  3.  * 1. Dimension:
  4.  *              0 = Anzahl der Zeilen
  5.  *              1 = Zeilenausgabe
  6.  *                      0 = Zeile 1
  7.  *                              0 = Spalte 1
  8.  *                              1 = Spalte 2
  9.  *      2 = AutoInkrement ID
  10.  * 00 Mysql Error
  11.  * 01 Connection Error
  12.  * 02 SqL Syntax Error
  13.  *                    
  14.  */
  15.     global $mysrv,$myusr,$mypass,$mysqldb,$debug;
  16.     $conn = new mysqli($mysrv,$myusr , $mypass,$mysqldb);
  17.     $trans=array();
  18.     // check connection
  19.     if ($conn->connect_error) {
  20.         trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
  21.     }
  22.     $rs=$conn->query($sql);
  23.     $id=$conn->insert_id;
  24.     if($rs === false) {
  25.      trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
  26.         } else {
  27.             $anz=(is_object($rs))?$rs->num_rows:0;
  28.             $i=0;
  29.             if($anz>0){
  30.             while ($row = $rs->fetch_assoc()) {
  31.          $arr[$i] = $row;
  32.                 $i++;
  33.             }
  34.             }else{
  35.                 $arr=array();
  36.             }
  37.             $trans[0]=$anz;
  38.             $trans[1]=$arr;
  39.             $trans[2]=$id;
  40.             $conn->close();
  41.             return $trans;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment