Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [HTML][PHP][MySQL]Proste(?) przerobienie skryptu msql na 2 tabele
fardbleecker
post
Post #1





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 24.12.2012

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


Witam, mam problem. Jestem totalnie obcy w php jak i mysql.
Potrzebuje przerobienia skryptu, który pobierał nazwe i stan konta, na taki który pobiera to z 2 różnych tabel. W jednej jest nazwa i id, w drugiej jest id i balans.

Stary skrypt
  1. <html>
  2. <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
  3. </html>
  4.  
  5. <?php
  6. require ('config.php');
  7. //Connects to the database and the specified table.
  8. $handle = mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die($connect_error);
  9. mysql_select_db($mysql_db, $handle) or die($connect_error);
  10. $sql = mysql_query('SELECT username, balance FROM '.$mysql_table.' ORDER BY balance DESC');
  11. //Define first rank.
  12. $rank = 1;
  13. ?>
  14.  
  15. <center>
  16. <h1><u>iConomy Player Stats</u></h1>
  17. <table border="0" cellpadding="3" cellspacing="10" id="minimalist">
  18. <tr>
  19. <td width="50"><strong>Avatar</strong></td>
  20. <td width="30"><strong>Username</strong></td>
  21. <td width="30"><strong>Wealth</strong></td>
  22. <td width="30"><strong>Rank</strong></td>
  23. </tr>
  24. <?php
  25. while($data = mysql_fetch_assoc($sql)) {
  26. //Loops data and displays it in a table.
  27. echo '<tr>';
  28. echo '<td><strong>'.'<img src=http://minotar.net/avatar/'.$data['id'].'/50.png />'.'</strong></td>';
  29. echo '<td><strong>'.$data['username'].'</strong></td>';
  30. echo '<td><strong>'.$data["balance"].'</strong></td>';
  31. echo '<td><strong>'.$rank++.'</strong></td></tr>';
  32. }
  33. ?>
  34. <?//Awkward ?>
  35. </table></center>


w config.php są dane oraz
  1. if (!mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!mysql_select_db($mysql_db)) {
  2. die($connect_error);


Z góry dziękuje!

Ten post edytował fardbleecker 24.12.2012, 22:05:48
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
piotrex41
post
Post #2





Grupa: Zarejestrowani
Postów: 168
Pomógł: 26
Dołączył: 15.09.2011
Skąd: Wrocław

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


Po 1. to obie tabele muszą mieć coś wspólnego (np. ID 1 w pierwszej tabeli odpowiada ID w drugiej tabeli).
Jeśli tak masz to spokojnie możesz zrobić to przy użyciu join'a. Poczytaj o "LEFT JOIN", "RIGHT JOIN" i "INNER JOIN" w MySQLu. Na prawdę łatwo, szybko i przyjemnie to zrobisz (IMG:style_emoticons/default/smile.gif)

Jeszcze mi się przypomniało, że kolega mówił mi coś o zagnieżdżaniu SELECT w SELECT, ale nie wiem do końca jak on to robił, bo wydawało mi się to nie optymalne rozwiązanie (IMG:style_emoticons/default/smile.gif)

Przykłady do obu:
1. Zagnieżdżone:
  1. SELECT nazwa
  2. FROM towary
  3. WHERE ID NOT IN (SELECT DISTINCT ID_towaru
  4. FROM zamowienia);

2. Z użyciem joina:
  1. SELECT towary.nazwa
  2. FROM towary
  3. LEFT OUTER JOIN zamowienia ON towary.ID=zamowienia.ID_towaru
  4. WHERE zamowienia.ID_towaru IS NULL;

albo
  1. SELECT towary.nazwa
  2. FROM towary, zamowienia
  3. WHERE towary.ID_towaru = zamowienia.ID_towaru;


Ten post edytował piotrex41 24.12.2012, 22:49:36
Go to the top of the page
+Quote Post
fardbleecker
post
Post #3





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 24.12.2012

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


o tak?

  1. SELECT name, balance
  2. FROM '.$mysql_table.', '.$mysql_table2.'
  3. WHERE '.$mysql_table.id' = '.$mysql_table2.id'


Ten post edytował fardbleecker 24.12.2012, 23:03:24
Go to the top of the page
+Quote Post
piotrex41
post
Post #4





Grupa: Zarejestrowani
Postów: 168
Pomógł: 26
Dołączył: 15.09.2011
Skąd: Wrocław

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


Chyba raczej jakoś tak:
  1. SELECT `'.$mysql_table.'.name`, `'.$mysql_table2.'.balance`
  2. FROM `'.$mysql_table.'`, `'.$mysql_table2.'`
  3. WHERE `'.$mysql_table.'.id` = `'.$mysql_table2.'.id`


Ten post edytował piotrex41 24.12.2012, 23:24:45
Go to the top of the page
+Quote Post
fardbleecker
post
Post #5





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 24.12.2012

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


  1. <html>
  2. <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
  3. </html>
  4.  
  5. <?php
  6. require ('config.php');
  7. //Connects to the database and the specified table.
  8. $handle = mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die($connect_error);
  9. mysql_select_db($mysql_db, $handle) or die($connect_error);
  10. $sql = mysql_query('
  11. SELECT `'.$mysql_table.'.name`, `'.$mysql_table2.'.balance`
  12. FROM `'.$mysql_table.'`, `'.$mysql_table2.'`
  13. WHERE `'.$mysql_table.'.id` = `'.$mysql_table2.'.id`
  14. ');
  15. //Define first rank.
  16. $rank = 1;
  17. ?>
  18.  
  19. <center>
  20. <h1><u>iConomy Player Stats</u></h1>
  21. <table border="0" cellpadding="3" cellspacing="10" id="minimalist">
  22. <tr>
  23. <td width="50"><strong>Avatar</strong></td>
  24. <td width="30"><strong>Username</strong></td>
  25. <td width="30"><strong>Wealth</strong></td>
  26. <td width="30"><strong>Rank</strong></td>
  27. </tr>
  28. <?php
  29. while($data = mysql_fetch_assoc($sql)) {
  30. //Loops data and displays it in a table.
  31. echo '<tr>';
  32. echo '<td><strong>'.'<img src=http://minotar.net/avatar/'.$data['id'].'/50.png />'.'</strong></td>';
  33. echo '<td><strong>'.$data['name'].'</strong></td>';
  34. echo '<td><strong>'.$data["balance"].'</strong></td>';
  35. echo '<td><strong>'.$rank++.'</strong></td></tr>';
  36. }
  37. ?>
  38. <?//Awkward ?>
  39. </table></center>


  1. Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fard/public_html/enjoycraft/conomy/index.php on line 29


Pewnie gdzieś błąd w formatowaniu ;/

Wie ktoś może gdzie jest błąd?

Znalazłem! Powinno być
  1. SELECT name, balance
  2. FROM '.$mysql_table.', '.$mysql_table2.'
  3. WHERE '.$mysql_table.'.`username_id` = '.$mysql_table2.'.`id`


Dzięki za pomoc (IMG:style_emoticons/default/smile.gif)

Ten post edytował fardbleecker 24.12.2012, 23:33:40
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 6.10.2025 - 01:21