Guest User

Untitled

a guest
Jan 30th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. function open_mysql_connection(){
  2. global $openconn,$conn;
  3. $servername = "p:1.1.1.1";
  4. $username = "demo";
  5. $password = "demo";
  6.  
  7. $dbname = "demo";
  8.  
  9. // Create connection
  10. $conn = new mysqli($servername, $username, $password, $dbname);
  11. // Check connection
  12. if ($conn->connect_error) {
  13. die("Connection failed: " . $conn->connect_error);
  14. }
  15.  
  16. /* change character set to utf8 */
  17. $conn->set_charset("utf8");
  18. $openconn = true;
  19. }
  20.  
  21. function close_mysql_connection(){
  22. global $openconn,$conn;
  23. if ($openconn){
  24. $conn->close();
  25. $openconn = false;
  26. }
  27. }
  28.  
  29. function get_mysql_query($query,$prepare=NULL,$prepare_type=NULL){
  30. global $openconn,$conn,$memcache,$LASTID;
  31.  
  32. if (!$openconn){
  33. open_mysql_connection();
  34. }
  35. if ($prepare!=NULL){
  36. $sql =$query;
  37. $a_bind_params =$prepare;
  38. $a_param_type =$prepare_type;
  39.  
  40. /* Bind parameters. Types: s = string, i = integer, d = double, b = blob */
  41. $a_params = array();
  42.  
  43. $param_type = '';
  44. $n = count($a_param_type);
  45. for($i = 0; $i < $n; $i++) {
  46. $param_type .= $a_param_type[$i];
  47. }
  48.  
  49. /* with call_user_func_array, array params must be passed by reference */
  50. $a_params[] = & $param_type;
  51.  
  52. for($i = 0; $i < $n; $i++) {
  53. /* with call_user_func_array, array params must be passed by reference */
  54. $a_params[] = & $a_bind_params[$i];
  55. }
  56.  
  57. /* Prepare statement */
  58. $stmt = $conn->prepare($sql);
  59. if($stmt === false) {
  60. trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->errno . ' ' . $conn->error, E_USER_ERROR);
  61. }
  62.  
  63. /* use call_user_func_array, as $stmt->bind_param('s', $param); does not accept params array */
  64. call_user_func_array(array($stmt, 'bind_param'), $a_params);
  65.  
  66. /* Execute statement */
  67. $stmt->execute();
  68.  
  69. // /* Store the result (to get properties) */
  70. // $stmt->store_result();
  71.  
  72. /* Fetch result to array */
  73. $result = $stmt->get_result(); // altenative: $stmt->bind_result($row_AcousticDB);
  74.  
  75. $LASTID = $stmt->insert_id;
  76. $stmt->free_result();
  77. $stmt->close();
  78. close_mysql_connection();
  79. return $result;
  80. }else{
  81. $result =$conn->query($query,MYSQLI_STORE_RESULT);
  82. $LASTID = $conn->insert_id;
  83. close_mysql_connection();
  84. return $result;
  85. }
  86. };
  87.  
  88.  
  89. $query_terms[]=$_GET["p"];
  90. $prepare_type[]='i';
  91. $query =" SELECT * from `tbl_post` where id = ? ";
  92. $WebResult = get_mysql_query($query,$query_terms,$prepare_type);
  93. if ($WebResult->num_rows > 0) {
  94. while($row = $WebResult->fetch_assoc()) {
  95. echo $row['ID'];
  96. }
  97. }
Add Comment
Please, Sign In to add comment