Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: problem z utworzeniem tabel
Forum PHP.pl > Forum > Bazy danych > MySQL
laszlo
Witam mam zgoła banalny problem przy utworzeniu tabel w bazie. Tworze baze i w niej chce utworzyc dwie tabele. Chce to zrobic wlasnie poprzez dolaczanie do zmiennej, lecz skrypt tworzy mi tylko baze a nie tworzy w niej tabel. Moze komus uda sie wychwycic blad w tym skrypcie, albo chociaz powiedziec mi czemu to nie dziala.

  1. <?php
  2. //include "./common_db.inc";
  3.  
  4. $dbhost ='localhost';
  5. $dbusername = 'phpuser';
  6. $dbuserpassword = 'phppass';
  7. $default_dbname = 'mysql';
  8. $link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
  9.  
  10.  
  11. $dbname = "sampl_db";
  12. $user_tablename = 'user';
  13. $user_table_def = "usernumber mediumint(10) default '0' NOT NULL auto_increment,";
  14. $user_table_def .= "userid varchar(8) NOT NULL,";
  15. $user_table_def .= "userpassword varchar(20) BINARY NOT NULL,";
  16. $user_table_def .= "username varchar(30) NOT NULL,";
  17. $user_table_def .= "usercountry varchar(50) NOT NULL,";
  18. $user_table_def .= "usermail varchar(30) NOT NULL,";
  19. $user_table_def .= "userprofile text NOT NULL,";
  20. $user_table_def .= "registerdate date default '0000-00-00' NOT NULL,";
  21. $user_table_def .= "lastaccesstime timestamp(14),";
  22. $user_table_def .= "PRIMARY KEY(userid),";
  23. $user_table_def .= "UNIQUE usernumber(usernumber)";
  24.  
  25. $access_log_tablename = "access_log";
  26. $access_log_table_def = "page varchar(250) NOT NULL";
  27. $access_log_table_def .= "userid varchar(8) BINARY NOT NULL";
  28. $access_log_table_def .= "visitcount mediumint(5) default '0' NOT NULL";
  29. $access_log_table_def .= "accessdate timestamp(14), KEY page(page)";
  30. $access_log_table_def .= "PRIMARY KEY(userid, page)";
  31.  
  32.  
  33. if(!$link_id)
  34.  echo "Nie udalo się nawiazac polaczenia z $dbhost.";
  35.  
  36. if (!mysql_query("CREATE DATABASE $dbname"))
  37.  die("blad utworzenia bazy");
  38.  
  39. echo "Udalo się stworzyc baze danych $dbname <br>";
  40.  
  41. if (!mysql_select_db($dbname)) die("blad wybrania");
  42.  echo "Udalo się wybrac baze $dbname<br>";
  43.  
  44. if (!mysql_query("CREATE TABLE $user_tablename($user_table_def)"))
  45.  die("blad utworzenia tabeli");
  46.  
  47. if (!mysql_query("CREATE TABLE $access_log_tablename($access_log_table_def)"))
  48. die("blad utworzenia tabeli");
  49.  
  50.  
  51. echo "Udalo się utworzyc tabele $user_tablename oraz $access_log_tablename";
  52.  
  53. ?>


poprawiam
---
nospor


?>
tts
uzyj mysql_error() i zobacz jaki blad zwroci zapytanie tworzace tabele
laszlo
Serwer zwraca takie zapytanie

Udalo się stworzyc baze danych sampl_db
1064: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULLuserid VARCHAR(8) BINARY NOT NULLvisitcount MEDIUMINT(5) DE
mike
Cytat(laszlo @ 2006-02-22 13:34:40)
1064: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULLuserid VARCHAR(8) BINARY NOT NULLvisitcount MEDIUMINT(5) DE (...)

I jaki wniosek?
Nie masz spacji
  1. <?php
  2.  
  3. //...
  4. // spacje pomiędzy kolejnymi częściami zapytania
  5. $user_table_def .= "userpassword varchar(20) BINARY NOT NULL, "; 
  6. $user_table_def .= "username varchar(30) NOT NULL, ";
  7. $user_table_def .= "usercountry varchar(50) NOT NULL, ";
  8. $user_table_def .= "usermail varchar(30) NOT NULL, ";
  9. $user_table_def .= "userprofile text NOT NULL, ";
  10. //...
  11.  
  12. ?>

i przecinków
  1. <?php
  2.  
  3. //...
  4. $access_log_table_def .= "userid varchar(8) BINARY NOT NULL, ";
  5. $access_log_table_def .= "visitcount mediumint(5) default '0' NOT NULL, ";
  6. $access_log_table_def .= "accessdate timestamp(14), KEY page(page), ";
  7. //...
  8.  
  9. ?>
laszlo
dzięki!!!! działa.
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.