Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Wywyływanie artykułu z bazy
Forum PHP.pl > Forum > PHP
Lethys
Postanowiłem że zrobię sobie skrypt gdzie bede umieszczał artykuły. Wymagań dużych nie mam, chodzi o to żeby nie tworzyć pliku HTML dla każdego artykułu a po prostu pobierać je z bazy danych.

I tak przykładowo w bazie mam 3 artykuły:




Artykuły działają z takich adresów:

name.php?name=balls
name.php?name=furniture
name.php?name=test

Skrypt:

  1. <?php
  2. include "config.php";
  3. $name = mysql_fetch_array(mysql_query("select * from articles where name='$name'"));
  4. $articles = mysql_fetch_array(mysql_query("select * from articles"));
  5.  
  6. if ($name[name] != $articles[name]){
  7.  
  8. print "We don't have this article. You have wrote propably wrong website adress.";
  9.  
  10. }else{
  11. mysql_query("update articles set visits=visits+1 where id=$name[id]");
  12.  
  13. $ip=$_SERVER['REMOTE_ADDR'];
  14.  
  15.  
  16. $test = mysql_num_rows(mysql_query("select * from ip where id='$name[id]' and ip='$ip'"));
  17. if ($test > 0) {
  18.  
  19. }else{
  20.  
  21.  
  22. mysql_query("update articles set uu=uu+1 where id=$name[id]");
  23. mysql_query("insert into ip (id, ip) values('$name[id]','$ip')");
  24. }
  25.  
  26.  
  27. print "Name:$name[article]<br>$name[article]";
  28.  
  29. }
  30.  
  31.  
  32.  
  33. ?>


I teraz najlepsze, działa tylko i wyłącznie wywołanie pierwsze czyli: name.php?name=balls, przy reszcie wyskakuje "We don't have this article. You have wrote propably wrong website adress."

Dam jeszcze plik .htaccess który może mieć wpływ na to dziwne zachowanie.

  1. php_flag register_globals on
  2. RewriteEngine on
  3. RewriteCond %{HTTP_HOST} ^mojastrona.com
  4. RewriteRule (.*) <a href="http://www.mojastrona.com/$1" target="_blank">http://www.mojastrona.com/$1</a> [R=301,L]
  5. RewriteBase /
  6. RewriteRule ^(.*)/$ name.php?name=$1 [NC]
  7.  



Wie ktoś dlaczego działa wywołanie tylko pierwszego artykułu ?

Jak już nie mam pojęcia co jest nie tak sad.gif
user_php.pl
Spróbuj użyć while zamiast if.
CuteOne
po co dwa razy pobierasz te same dane? mówię o:
  1. $name = mysql_fetch_array(mysql_query("select * from articles where name='$name'"));
  2. $articles = mysql_fetch_array(mysql_query("select * from articles"));


wystarczy samo $articles..
  1. <?php
  2. include "config.php";
  3.  
  4. $name = mysql_real_escape_string($_GET['name']);
  5.  
  6. $query = mysql_query("select * from articles where name='$name'");
  7. $articles = mysql_fetch_array($query);
  8.  
  9. if (!mysql_num_rows($query)){
  10.  
  11. print "We don't have this article. You have wrote propably wrong website adress.";
  12.  
  13. }else{
  14. echo $articles['name'];
  15. //..........
Lethys
Z while wogole nie dziala.

@CuteOne

Kiedy zrobie Twoim sposobem i skrypt wyglada tak :

  1. <?php
  2. include "config.php";
  3. $name = mysql_real_escape_string($_GET['name']);
  4.  
  5. $query = mysql_query("select * from articles where name='$name'");
  6. $articles = mysql_fetch_array($query);
  7.  
  8. if (!mysql_num_rows($query)){
  9.  
  10. print "We don't have this article. You have wrote propably wrong website adress.";
  11.  
  12. }else{
  13. mysql_query("update articles set visits=visits+1 where id=$name[id]");
  14.  
  15. $ip=$_SERVER['REMOTE_ADDR'];
  16.  
  17.  
  18. $test = mysql_num_rows(mysql_query("select * from ip where id='$name[id]' and ip='$ip'"));
  19. if ($test > 0) {
  20.  
  21. }else{
  22.  
  23.  
  24. mysql_query("update articles set uu=uu+1 where id=$name[id]");
  25. mysql_query("insert into ip (id, ip) values('$name[id]','$ip')");
  26. }
  27.  
  28.  
  29. print "Name:$name[article]<br>$name[article]";
  30.  
  31. }
  32.  
  33.  
  34.  
  35. ?>


W princie pokazuje tylko pierwsze litery wartości.

Czyli z linku balls jako $name[article] pokaze tylko"b"

o co chodzi ?

czemu nie pojawiaja sie wartosci z bazy?
CuteOne
a powiedz mi skąd ty bierzesz to $name[article]questionmark.gif - powinno być $articles['name']

  1. print "Name:$articles['name']";


Po drugie używaj apostrofów w indeksach - $name['article']
Po trzecie lepszym wyjściem jest używanie echo zamiast print
Lethys
Zmieniłem i poprawiłem wszystko co mówiłeś, ale zamiast wyświetlać artykuł teraz nic się nie dzieje. Pojawia się po prostu biała pusta strona.

Nawet jeżeli wpisze się name=qweqwre czyli coś czego nie ma w bazie to już nie wyświetla informacji że nie ma takiego artykułu tylko pojawia się ta pusta strona.


Kod wygląda tak:

  1. <?php
  2.  
  3. error_reporting(E_STRICT);
  4. ini_set('display_errors', 1);
  5. include "config.php";
  6. $name = mysql_real_escape_string($_GET['name']) or mysql_error();
  7.  
  8. $query = mysql_query("select * from articles where name='$name'") or mysql_error();
  9. $articles = mysql_fetch_array($query);
  10.  
  11. if (!mysql_num_rows($query)){
  12.  
  13. echo "We don't have this article. You have wrote propably wrong website adress.";
  14.  
  15. }else{
  16. mysql_query("update articles set visits=visits+1 where id=$name['id']");
  17.  
  18. $ip=$_SERVER['REMOTE_ADDR'];
  19.  
  20.  
  21. $test = mysql_num_rows(mysql_query("select * from ip where id='$name['id']' and ip='$ip'"));
  22. if ($test > 0) {
  23.  
  24. }else{
  25.  
  26.  
  27. mysql_query("update articles set uu=uu+1 where id=$name['id']");
  28. mysql_query("insert into ip (id, ip) values('$name['id']','$ip')");
  29. }
  30.  
  31.  
  32. echo "Name:$articles['title']<br>$articles['article']";
  33.  
  34. }
  35.  
  36.  
  37.  
  38. ?>
krowal
Żeby było jasne, piszę o wersji kodu z pierwszego posta. Pochrzaniłeś wszystko jeśli chodzi o wybieranie rekordu z bazy. Najpierw wybierasz potrzebny rekord a potem chcesz jeszcze wybrać wszystkie rekordy i sprawdzić czy ten pierwszy znajduje się w zbiorze z tego drugiego zapytania ? Już nie mówiąc o tym, że wybieranie wszystkich rekordów robisz źle. Zapytanie jest dobre, ale aby mieć wszystkie rekordy w tablicy musisz wykonać funkcje mysql_fetch_array() podając jej jako parametr to co zwróci mysql_query() a to wszystko w pętli, aż mysql_fetch_array() wyciągnie ze źródła mysql wszystkie wybrane wiersze i wrzuci je do tablicy.

Imo w zupełności wystarczy tak:
  1. include "config.php";
  2. $name = mysql_fetch_array(mysql_query("select * from articles where name='$name'"));
  3.  
  4. if (!$name) {
  5.  
  6. print "We don't have this article. You have wrote propably wrong website adress.";
  7. } else {
  8. mysql_query("update articles set visits=visits+1 where id=$name[id]");
  9.  
  10. $ip = $_SERVER['REMOTE_ADDR'];
  11.  
  12.  
  13. $test = mysql_num_rows(mysql_query("select * from ip where id='$name[id]' and ip='$ip'"));
  14. if ($test > 0) {
  15.  
  16. } else {
  17.  
  18.  
  19. mysql_query("update articles set uu=uu+1 where id=$name[id]");
  20. mysql_query("insert into ip (id, ip) values('$name[id]','$ip')");
  21. }
  22.  
  23.  
  24. print "Name:$name[article]<br>$name[article]";
  25. }
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.