Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Pobranie dwóch tabel
Forum PHP.pl > Forum > Przedszkole
tenloginjestzajety
Witam mam problem, nie wiem jak pobrać dwie tabele, do pobrania jednej używam:
  1. $query1 = mysql_query("SELECT DISTINCT `tag` FROM `comments`");
  2. while($row = mysql_fetch_assoc($query1)){echo '';}


Potrzebuje jeszcze pobrać:

  1. $query1 = mysql_query("SELECT DISTINCT `tag` FROM `tags`");
  2. while($row = mysql_fetch_assoc($query1)){echo '';}


Tak żeby w jednym echo były wniki z dwóch tabel. Dziękuje
kapslokk
  1. SELECT tag FROM tags UNION SELECT tag FROM comments GROUP BY tag

Ja to widzę tak smile.gif
tenloginjestzajety
dzięki, mam jeszcze jeden problem. Dla całego serwisu używam ustawień sql z jednego pliku. Chciałbym wstawić fragment kodu, który będzie pobierał dane z innej bazy, w tym celu wstawiam:

  1. include ('config2.php');
  2. $sql = new MySQL();


pojawia się taki błąd:

Fatal error: Cannot redeclare opt() (previously declared in /home/config.php )
pozdrawiam
kapslokk
W config i w config2 masz dwie funkcje o tej samej nazwie, wydaje mi się, że namespace załatwi problem. Ale chyba w tym przypadku będzie lepiej przepisać te configi, tak aby korzystały z jednego zestawu funkcji, a żeby tylko wartości parametrów były inne smile.gif
tenloginjestzajety
  1. <?php
  2.  
  3. define('_DB_NAME_', 'xxx');
  4.  
  5. define('_DB_SERVER_', 'xxx');
  6.  
  7. define('_DB_USER_', 'xxx');
  8.  
  9. define('_DB_PASSWD_', 'xxx');
  10.  
  11.  
  12. class MySQL{
  13.  
  14. var $m;
  15.  
  16. var $result;
  17.  
  18. public $errors = array();
  19.  
  20. public function __construct(){
  21.  
  22. $this->m = mysql_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_);
  23.  
  24. $this->query('SET charset utf8');
  25.  
  26. $this->errors = array(mysql_errno($this->m), mysql_error($this->m));
  27.  
  28. }
  29.  
  30. public function query($sql){
  31.  
  32. $this->result = mysql_db_query(_DB_NAME_, $sql, $this->m);
  33.  
  34. $this->errors = array(mysql_errno($this->m), mysql_error($this->m));
  35.  
  36. }
  37.  
  38. public function num_rows(){
  39.  
  40. return mysql_num_rows($this->result);
  41.  
  42. }
  43.  
  44. public function affected_rows(){
  45.  
  46. return mysql_affected_rows($this->m);
  47.  
  48. }
  49.  
  50. public function sh_result(){
  51.  
  52. return $this->result;
  53.  
  54. }
  55.  
  56. public function id(){
  57.  
  58. return mysql_insert_id($this->m);
  59.  
  60. }
  61.  
  62. public function escape($string){
  63.  
  64. return mysql_real_escape_string(trim($string), $this->m);
  65.  
  66. }
  67.  
  68. public function fetch_array(){
  69.  
  70. return mysql_fetch_array($this->result, MYSQL_ASSOC);
  71.  
  72. }
  73.  
  74. public function result(){
  75.  
  76. return mysql_result($this->result, 0);
  77.  
  78. }
  79.  
  80. public function __destruct(){
  81.  
  82. $this->errors = array(mysql_errno($this->m), mysql_error($this->m));
  83.  
  84. //mysql_close($this->m);
  85.  
  86. }
  87.  
  88. }
  89.  
  90.  
  91. ?>


Drugi config rózni się jedynie danymi do sql. Nie bardzo wiem jak to ogarnąć.
kapslokk
db.class.php
  1. <?php
  2. class MySQL{
  3.  
  4. var $m;
  5.  
  6. var $result;
  7. private $server;
  8. private $user;
  9. private $password;
  10. private $database;
  11.  
  12. public $errors = array();
  13.  
  14. public function __construct($server, $user, $password, $database){
  15. $this->server = $server;
  16. $this->user = $user;
  17. $this->password = $password;
  18. $this->database = $database;
  19.  
  20. $this->m = mysql_connect($this->server, $this->user, $this->password);
  21.  
  22. $this->query('SET charset utf8');
  23.  
  24. $this->errors = array(mysql_errno($this->m), mysql_error($this->m));
  25.  
  26. }
  27.  
  28. public function query($sql){
  29.  
  30. $this->result = mysql_db_query($this->database, $sql, $this->m);
  31.  
  32. $this->errors = array(mysql_errno($this->m), mysql_error($this->m));
  33.  
  34. }
  35.  
  36. public function num_rows(){
  37.  
  38. return mysql_num_rows($this->result);
  39.  
  40. }
  41.  
  42. public function affected_rows(){
  43.  
  44. return mysql_affected_rows($this->m);
  45.  
  46. }
  47.  
  48. public function sh_result(){
  49.  
  50. return $this->result;
  51.  
  52. }
  53.  
  54. public function id(){
  55.  
  56. return mysql_insert_id($this->m);
  57.  
  58. }
  59.  
  60. public function escape($string){
  61.  
  62. return mysql_real_escape_string(trim($string), $this->m);
  63.  
  64. }
  65.  
  66. public function fetch_array(){
  67.  
  68. return mysql_fetch_array($this->result, MYSQL_ASSOC);
  69.  
  70. }
  71.  
  72. public function result(){
  73.  
  74. return mysql_result($this->result, 0);
  75.  
  76. }
  77.  
  78. public function __destruct(){
  79.  
  80. $this->errors = array(mysql_errno($this->m), mysql_error($this->m));
  81.  
  82. //mysql_close($this->m);
  83.  
  84. }
  85.  
  86. }


i później:
  1. $mysql1 = new MySQL('serwer','uzytkownik', 'haslo', 'nazwa_bazy');
  2. $mysql2 = new MySQL('serwer2','uzytkownik2', 'haslo2', 'nazwa_bazy2');
tenloginjestzajety
dziękuje, ale teraz błąd:


W index.php wstawiam:
  1. include('config2.php');
  2. $mysql1 = new MySQL('xxx','xxx', 'xxx', 'xxx');

Działa.

  1. include('config2.php');
  2. $mysql2 = new MySQL('xxx','xxx', 'xxx', 'xxx');

Fatal error: Cannot redeclare class MySQL in /home/config2.php on line 2
tomxx
Cytat(tenloginjestzajety @ 4.07.2015, 22:55:45 ) *
dziękuje, ale teraz błąd:


W index.php wstawiam:
  1. include('config2.php');
  2. $mysql1 = new MySQL('xxx','xxx', 'xxx', 'xxx');

Działa.

  1. include('config2.php');
  2. $mysql2 = new MySQL('xxx','xxx', 'xxx', 'xxx');

Fatal error: Cannot redeclare class MySQL in /home/config2.php on line 2

Wobec tego gdzieś masz dwa razy zadeklarowaną klasę, sprawdź wszystkie pliki, które tu includujesz i które includujesz w pliku config2.
tenloginjestzajety
działa! Nie potrzebnie dałem drugi raz include. Dziękuje wszystkim za pomoc.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.