Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. public function getEntity($tableName, $fieldList, $keyValueArray = NULL)
  2. {
  3. // First construct the sql to be used to get the entity
  4. if(is_array($fieldList))
  5. {
  6. $sql = "SELECT ";
  7. foreach($fieldList as $field)
  8. {
  9. $sql .= $field." , ";
  10. }
  11. $sql = rtrim($sql, ' , ');
  12. }
  13. else
  14. {
  15. $sql = "SELECT ".$fieldList;
  16. }
  17. $sql .= " FROM ".$tableName;
  18. if($keyValueArray != NULL)
  19. {
  20. $sql .= " WHERE ";
  21. foreach($keyValueArray as $key => $value)
  22. {
  23. $sql .= $key." = ? AND ";
  24. }
  25. $sql = rtrim($sql, ' AND ');
  26. }
  27. //Then grab the connection
  28. $connection = $this->getConnection();
  29. //Prepare the statement
  30. $stmt = $connection->prepare($sql);
  31. if($stmt === false)
  32. {
  33. trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $connection->errno . ' ' . $connection->error, E_USER_ERROR);
  34. }
  35. // Bind the paramaters
  36. if($keyValueArray != NULL AND count($keyValueArray) < 2)
  37. {
  38. $stmt->bind_param("s", $keyValueArray[key($keyValueArray)]);
  39. }
  40. elseif($keyValueArray != NULL)
  41. {
  42. $valueArray = array();
  43. $string = NULL;
  44. for($i = 0; $i < count($keyValueArray); $i++)
  45. {
  46. $string .= "s"; // for temp solution
  47. }
  48. $valueArray[] = &$string;
  49. foreach($keyValueArray as $key => $value)
  50. {
  51. $valueArray[] = &$keyValueArray[$key];
  52. }
  53. call_user_func(array($stmt, 'bind_param'), $valueArray);
  54. }
  55. //Execute the statement
  56. $stmt->execute();
  57. //Grab the results and stuff into multidimensional array
  58. $data = $stmt->result_metadata();
  59. $fields = array();
  60. $currentrow = array();
  61. $results = array();
  62. //Store references to keys in $currentrow
  63. while ($field = mysqli_fetch_field($data))
  64. {
  65. $fields[] = &$currentrow[$field->name];
  66. }
  67. // Bind statement to $currentrow using the array of references
  68. call_user_func_array(array($stmt, 'bind_result'), $fields);
  69. // Iteratively refresh $currentrow array using "fetch", store values from each row in $results array
  70. $i = 0;
  71. while ($stmt->fetch())
  72. {
  73. $results[$i] = array();
  74. foreach($currentrow as $key => $val)
  75. {
  76. $results[$i][$key] = $val;
  77. }
  78. $i++;
  79. }
  80. $connection->close();
  81. return $results;
  82. }
  83.  
  84. $keyValueArray['firstName'] = 'John';
  85. $keyValueArray['lastName'] = 'Doe';
  86. $fieldListArray[] = 'firstName';
  87. $fieldListArray[] = 'lastName';
  88. print_r($db->getEntity('ACCOUNT', $fieldListArray, $keyValueArray));
  89.  
  90. Wrong parameter count for mysqli_stmt::bind_param()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement