Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP]Pobranie dwóch tabel
tenloginjestzaje...
post 4.07.2015, 19:03:29
Post #1





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


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


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post
kapslokk
post 4.07.2015, 19:15:43
Post #2





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


  1. SELECT tag FROM tags UNION SELECT tag FROM comments GROUP BY tag

Ja to widzę tak smile.gif
Go to the top of the page
+Quote Post
tenloginjestzaje...
post 4.07.2015, 20:38:42
Post #3





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


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


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post
kapslokk
post 4.07.2015, 21:09:47
Post #4





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post
tenloginjestzaje...
post 4.07.2015, 21:37:06
Post #5





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


  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ąć.

Ten post edytował tenloginjestzajety 4.07.2015, 21:38:08


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post
kapslokk
post 4.07.2015, 21:44:40
Post #6





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


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');


Ten post edytował kapslokk 4.07.2015, 21:46:35
Go to the top of the page
+Quote Post
tenloginjestzaje...
post 4.07.2015, 21:55:45
Post #7





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


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


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post
tomxx
post 4.07.2015, 21:59:31
Post #8





Grupa: Zarejestrowani
Postów: 172
Pomógł: 27
Dołączył: 5.10.2013

Ostrzeżenie: (0%)
-----


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.

Ten post edytował tomxx 4.07.2015, 22:00:34
Go to the top of the page
+Quote Post
tenloginjestzaje...
post 4.07.2015, 22:01:08
Post #9





Grupa: Zarejestrowani
Postów: 358
Pomógł: 1
Dołączył: 20.07.2014

Ostrzeżenie: (0%)
-----


działa! Nie potrzebnie dałem drugi raz include. Dziękuje wszystkim za pomoc.


--------------------
Nigdy nie zapominam kliknąć "pomógł". Zastosowanie na stronie [prosze nie spamuj]
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.04.2024 - 06:43