Advertisement
Guest User

PHP mySQLi examples

a guest
Mar 13th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <?php
  2.  
  3.     if (isset($_GET['username']))
  4.     {
  5.         $username=$_GET['username'];
  6.         if ($username === '')
  7.         {
  8.             exit;
  9.         }
  10.        
  11.         $host = 'localhost';
  12.         $user = 'user';
  13.         $pass = 'pass';
  14.         $db = 'db';
  15.         $table = 'forum_cookies';
  16.        
  17.         $mysqli = new mysqli($host,$user,$pass,$db);
  18.         if ($mysqli->connect_errno)
  19.         {
  20.             printf("Connect failed: (%s) %s\n", $mysqli->errno, $mysqli->connect_error);
  21.             exit;
  22.         }
  23.        
  24.        
  25.         //get one row example:
  26.         if ($result = $mysqli->prepare("SELECT * FROM $table WHERE username=?"))
  27.         {
  28.             $result->bind_param('s', $username);
  29.             $result->execute();
  30.             $result->bind_result($ri_username, $ri_date, $ri_user_id, $ri_p_cookies);
  31.             $result->fetch();
  32.             //do stuff with $ri_username, $ri_date, $ri_user_id, $ri_p_cookies here
  33.             $result->close();
  34.         }
  35.         else
  36.         {
  37.             printf("Query failed: (%s) %s\n", $mysqli->errno, $mysqli->error);
  38.             exit;
  39.         }
  40.        
  41.         //or:
  42.         if ($result = $mysqli->prepare("SELECT * FROM $table WHERE username=?"))
  43.         {
  44.             $result->bind_param('s', $username);
  45.             $result->execute();
  46.             $result->bind_result($ri_username, $ri_date, $ri_user_id, $ri_p_cookies);
  47.             $result->fetch();
  48.         }
  49.         else
  50.         {
  51.             printf("Query failed: (%s) %s\n", $mysqli->errno, $mysqli->error);
  52.             exit;
  53.         }
  54.         //do stuff with $ri_username, $ri_date, $ri_user_id, $ri_p_cookies here
  55.         $result->close();
  56.  
  57.    
  58.         //get more than one row example:
  59.         if ($result = $mysqli->prepare("SELECT * FROM $table WHERE sender=? OR receiver=?"))
  60.         {
  61.             $result->bind_param('ss', $username, $username);
  62.             $result->execute();
  63.             $result->bind_result($r_username, $r_target_username, $isupvote, $date);
  64.             $no_results=true;
  65.             while ($result->fetch())
  66.             {
  67.                 //do stuff with $r_username, $r_target_username, $isupvote, $date
  68.                 //in the next while passtrough, a new row will be loaded into those variables
  69.                 //or if you want to keep them all, load them into an array (there's a better function for this though, that does it all in one go)
  70.                 $no_results=false;
  71.             }
  72.             $result->close();
  73.         }
  74.         else
  75.         {
  76.             printf("Query failed: (%s) %s\n", $mysqli->errno, $mysqli->error);
  77.             exit;
  78.         }
  79.        
  80.        
  81.         //insert one row example
  82.         //with additional error checks (which have been omited in the above SELECT queries)
  83.         //you can include them if you want, but it's kinda superfluous, I just put them here as an example
  84.         if ($result = $mysqli->prepare("INSERT INTO $table VALUES (?,?,?,?)"))
  85.         {
  86.             if (!$result->bind_param('ssss', $username, $target_username, $isupvote, $date)) {
  87.                 printf("Binding parameters failed: (%s) %s\n", $stmt->errno, $stmt->error);
  88.                 exit;
  89.             }
  90.             if (!$result->execute()) {
  91.                 printf("Execute failed: (%s) %s\n", $stmt->errno, $stmt->error);
  92.                 exit;
  93.             }
  94.             $result->close();
  95.         }
  96.         else
  97.         {
  98.             printf("Query failed: (%s) %s\n", $mysqli->errno, $mysqli->error);
  99.             exit;
  100.         }
  101.        
  102.         //then after the last query:
  103.         $mysqli->close();
  104.     }
  105.    
  106.    
  107.    
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement