Guest User

Untitled

a guest
Jun 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. /*
  2. Similar to mysql_fetch_assoc(), this function returns an associative array
  3. given a mysql resource, but prepends the table name (or table alias, if
  4. used in the query) to the column name, effectively namespacing the column
  5. names and allowing SELECTS for column names that would otherwise have collided
  6. when building a row's associative array.
  7. */
  8. function mysql_fetch_assoc_with_table_names($resource) {
  9. // get a numerically indexed row, which includes all fields, even if their names collide
  10. $row = mysql_fetch_row($resource);
  11. if( ! $row)
  12. return $row;
  13.  
  14. $result = array();
  15.  
  16. $size = count($row);
  17. for($i = 0; $i < $size; $i++) {
  18. // now fetch the field information
  19. $info = mysql_fetch_field($resource, $i);
  20. $table = $info->table;
  21. $name = $info->name;
  22. // and make an associative array, where the key is $table.$name
  23. $result["$table.$name"] = $row[$i]; // e.g. $result["user.name"] = "Joe Schmoe";
  24. }
  25.  
  26. return $result;
  27. }
  28.  
  29. $resource = mysql_query("SELECT * FROM user JOIN question USING (user_id)");
  30. while($row = mysql_fetch_assoc_with_table_names($resource)) {
  31. echo $row['question.title'] . ' Asked by ' . $row['user.name'] . "n";
  32. }
  33.  
  34. select * from tabx tx, taby ty where ... ;
  35.  
  36. SELECT tabx.*, taby.* FROM tabx, taby WHERE ...
  37.  
  38. <?php
  39. $pdo = new PDO($dsn, $user, $pass, array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
  40. function aliasFields($pdo, $table, $delim='__') {
  41. $fields = array();
  42. // gotta sanitise the table name - can't do it with prepared statement
  43. $table = preg_replace('/[^A-z0-9_]/', "", $table);
  44. foreach ($pdo->query("EXPLAIN `".$table."`") as $row) {
  45. $fields[] = $table.'.'.$row['Field'].' as '.$table.$delim.$row['Field'];
  46. }
  47. return $fields;
  48. }
  49. $fieldAliases = array_merge(aliasFields($pdo, 'artist'), aliasFields($pdo, 'event'));
  50. $query = 'SELECT '.implode(', ', $fieldAliases).' FROM artist, event';
  51. echo $query;
  52.  
  53. // ABOVE PROGRAM'S OUTPUT (assuming database exists)
  54. SELECT artist__artist_id, artist__event_id, artist__artist_name, event__event_id, event__event_name FROM artist, event
  55.  
  56. <?php
  57.  
  58. $pdo = new PDO($dsn, $user, $pass, array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
  59.  
  60. $query = 'SELECT artist.*, eventartist.* FROM artist, eventartist LIMIT 1';
  61. $stmt = $pdo->prepare($query);
  62. $stmt->execute();
  63.  
  64. while ($row = $stmt->fetch()) {
  65. foreach ($row as $key=>$value) {
  66. if (is_int($key)) {
  67. $meta = $stmt->getColumnMeta($key);
  68. echo $meta['table'].".".$meta['name']."<br />";
  69. }
  70. }
  71. }
Add Comment
Please, Sign In to add comment