Guest User

Untitled

a guest
Dec 7th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. $query = "SELECT x FROM y WHERE z = $var";
  2. $result = mysql_query($query);
  3. while ($row = mysql_fetch_assoc($result)){
  4. echo $row['x'];
  5. }
  6.  
  7. $query = "SELECT x FROM y WHERE z = ?";
  8. $result = performQuery($query,"s",$var);
  9. while ($row = mysql_fetch_assoc($result)){
  10. echo $row['x'];
  11. }
  12.  
  13. function get_result( $Statement ) {
  14. $RESULT = array();
  15. $Statement->store_result();
  16. for ( $i = 0; $i < $Statement->num_rows; $i++ ) {
  17. $Metadata = $Statement->result_metadata();
  18. $PARAMS = array();
  19. while ( $Field = $Metadata->fetch_field() ) {
  20. $PARAMS[] = &$RESULT[ $i ][ $Field->name ];
  21. }
  22. call_user_func_array( array( $Statement, 'bind_result' ), $PARAMS );
  23. $Statement->fetch();
  24. }
  25. return $RESULT;
  26. }
  27.  
  28. $Statement = $Database->prepare( 'SELECT x FROM y WHERE z = ?' );
  29. $Statement->bind_param( 's', $z );
  30. $Statement->execute();
  31. $Result = $Statement->get_result();
  32. while ( $DATA = $Result->fetch_array() ) {
  33. // Do stuff with the data
  34. }
  35.  
  36. $Statement = $Database->prepare( 'SELECT x FROM y WHERE z = ?' );
  37. $Statement->bind_param( 's', $z );
  38. $Statement->execute();
  39. $RESULT = get_result( $Statement );
  40. while ( $DATA = array_shift( $RESULT ) ) {
  41. // Do stuff with the data
  42. }
  43.  
  44. function db_bind_array($stmt, &$row)
  45. {
  46. $md = $stmt->result_metadata();
  47. $params = array();
  48. while($field = $md->fetch_field()) {
  49. $params[] = &$row[$field->name];
  50. }
  51. return call_user_func_array(array($stmt, 'bind_result'), $params);
  52. }
  53.  
  54. function db_query($db, $query, $types, $params)
  55. {
  56. $ret = FALSE;
  57. $stmt = $db->prepare($query);
  58. call_user_func_array(array($stmt,'bind_param'),
  59. array_merge(array($types), $params));
  60. $stmt->execute();
  61.  
  62. $result = array();
  63. if (db_bind_array($stmt, $result) !== FALSE) {
  64. $ret = array($stmt, $result);
  65. }
  66.  
  67. $stmt->close();
  68. return $ret;
  69. }
  70.  
  71. $userId = $_GET['uid'];
  72. $sql = 'SELECT name, mail FROM users WHERE user_id = ?';
  73. if (($qryRes = db_query($db, $sql, 'd', array(&$userId))) !== FALSE) {
  74. $stmt = $qryRes[0];
  75. $row = $qryRes[1];
  76.  
  77. while ($stmt->fetch()) {
  78. echo '<p>Name: '.$row['name'].'<br>'
  79. .'Mail: '.$row['mail'].'</p>';
  80. }
  81. $stmt->close();
  82. }
  83.  
  84. class IImysqli_result {
  85. public $stmt, $ncols;
  86. }
  87.  
  88. class DBObject {
  89.  
  90. function iimysqli_get_result($stmt) {
  91. $metadata = $stmt->result_metadata();
  92. $ret = new IImysqli_result;
  93. if (!$ret || !$metadata) return NULL; //the latter because this gets called whether we are adding/updating as well as returning
  94. $ret->ncols = $metadata->field_count;
  95. $ret->stmt = $stmt;
  96. $metadata->free_result();
  97. return $ret;
  98. }
  99.  
  100. //this mimics mysqli_fetch_array by returning a new row each time until exhausted
  101. function iimysqli_result_fetch_array(&$result) {
  102. $stmt = $result->stmt;
  103. $stmt->store_result();
  104. $resultkeys = array();
  105. $thisName = "";
  106. for ( $i = 0; $i < $stmt->num_rows; $i++ ) {
  107. $metadata = $stmt->result_metadata();
  108. while ( $field = $metadata->fetch_field() ) {
  109. $thisName = $field->name;
  110. $resultkeys[] = $thisName;
  111. }
  112. }
  113.  
  114. $ret = array();
  115. $code = "return mysqli_stmt_bind_result($result->stmt ";
  116. for ($i=0; $i<$result->ncols; $i++) {
  117. $ret[$i] = NULL;
  118. $theValue = $resultkeys[$i];
  119. $code .= ", $ret['$theValue']";
  120. }
  121.  
  122. $code .= ");";
  123. if (!eval($code)) {
  124. return NULL;
  125. }
  126.  
  127. // This should advance the "$stmt" cursor.
  128. if (!mysqli_stmt_fetch($result->stmt)) {
  129. return NULL;
  130. }
  131.  
  132. // Return the array we built.
  133. return $ret;
  134. }
  135. }
Add Comment
Please, Sign In to add comment