Advertisement
Guest User

mysqlicode

a guest
Apr 6th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. <?php
  2. //usual db stuff
  3. $DBhost = 'localhost';
  4. $DBname = 'test';
  5. $DBuser = '';
  6. $DBpass = '';
  7.  
  8. //error reporting for mysql
  9. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  10.  
  11. //connect to database
  12. $conn = new mysqli($DBhost, $DBuser, $DBpass, $DBname);
  13.  
  14. //prepare this is a dummy table in a dummy database
  15. //actual table is quite complicated
  16. $stmt = $conn->prepare('INSERT INTO `users` (`fname`,`lname`,`city`) VALUES(?,?,?)');
  17.  
  18. //create a variable to store the data
  19. // so we can bind it before going further
  20. $data=array_fill(0,3,'');
  21. //bind params use array expansion
  22. $stmt->bind_param('sss', ...$data);
  23.  
  24. //this part runs in a loop
  25. // mock loop for just to show idea
  26. for($i=0;$i<1;$i++)
  27. {
  28.     //get the data
  29.     $data = get_data();
  30.     //already binded $data before the loop
  31.     //so execute this should insert the data just returned
  32.     //except this doesnt, it inserts blanks
  33.     //it acts as if the data still has the blank array from line 20
  34.     $stmt->execute();
  35. }
  36.  
  37. //mock function to return data the actual function
  38. //returns a multi dimesional array for multiple tables
  39. function get_data()
  40. {
  41.     return(array("dinesh","chand","nadi"));
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement