multiquery.lib.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. // get fields informations
  3. function multiquery_query($array)
  4. {
  5. $result = array();
  6. $field = 0;
  7. for ($i = 0; $i < sizeof($array); $i++) {
  8. // mysql handler
  9. $result[$i]['mysql'] = mysql_query($array[$i]);
  10. if (!$result[$i]['mysql']) {
  11. die("error in query $i : ".$array[$i]);
  12. }
  13. // fields
  14. $result[$i]['num_fields'] = mysql_num_fields($result[$i]['mysql']);
  15. for ($j = 0; $j < $result[$i]['num_fields']; $j++) {
  16. $name = mysql_field_name($result[$i]['mysql'], $j);
  17. $result['field'][$field]['query'] = $i;
  18. $result['field'][$field]['name'] = $name;
  19. $result['field'][$field]['id'] = $j;
  20. $result['field_assoc'][$name][$field];
  21. $field++;
  22. }
  23. }
  24. $result['num_queries'] = sizeof($array);
  25. // rows
  26. $numberOfResult = mysql_num_rows($result[0]['mysql']);
  27. for ($i = 1; $i < $result['num_queries']; $i++) {
  28. if ($numberOfResult != mysql_num_rows($result[$i]['mysql'])) {
  29. die("wrong number of row: $numberOfResult vs ".
  30. mysql_num_rows($result[$i]['mysql'])." on query $i");
  31. }
  32. }
  33. $result['num_rows'] = $numberOfResult;
  34. $result['num_fields'] = $field;
  35. return $result;
  36. }
  37. function multiquery_num_rows(&$mq_h)
  38. {
  39. return $mq_h['num_rows'];
  40. }
  41. function multiquery_num_fields(&$mq_h)
  42. {
  43. return $mq_h['num_fields'];
  44. }
  45. function multiquery_field_name(&$mq_h, $id)
  46. {
  47. return $mq_h['field'][$id]['name'];
  48. }
  49. function multiquery_fetch_row(&$mq_h)
  50. {
  51. $result = array();
  52. $pos = 0;
  53. for ($i = 0; $i < $mq_h['num_queries']; $i++) {
  54. $row = mysql_fetch_row($mq_h[$i]['mysql']);
  55. if (!$row) {
  56. return false;
  57. } // last line
  58. for ($j = 0; $j < sizeof($row); $j++) {
  59. $result[$pos] = $row[$j];
  60. $pos++;
  61. }
  62. }
  63. return $result;
  64. }