How do I create a table in a database? (PHP+MySQL)

Hello!

I found video lessons on the course Apache+PHP+MySQL on the Internet, but all the actions take place there, as I understand it, in Ukraine, although it tells in Russian. They told us how and gave the code for creating a database and tables in it.

Creating a table in the database Usersbd:

<?php
$Link = mysql_connect('localhost', 'root', '12345');

if(!$Link) echo "Не удалось подключится к серверу";
else
{
    mysql_select_db('Usersbd');

    $sql = "CREATE TABLE 'Users'  ('UserID'  VARCHAR(5) CHARACTER SET cp1251 COLLATE cp1251_ukrainian_ci NOT NULL ". 
        " 'Name'  VARCHAR(25) CHARACTER SET cp1251 COLLATE cp1251_ukrainian_ci, ". 
        " 'E-mail'  VARCHAR(25) CHARACTER SET cp1251 COLLATE cp1251_ukrainian_ci)";
    if (mysql_query($sql))
        echo "Создание таблицы завершено";
    else
        echo "Таблицу создать не удалось";
}
?>

character set cp1251 - the field supports Cyrillic encoding
collate cp1251_ukrainian_ci - how to compare the Cyrillic alphabet with the support of Ukrainian characters
not null - the field is not empty

Since I live in Russia - I don't need Ukrainian symbols. I tried to remove the comparison item, the reaction was unequivocal: "The table could not be created"...

I searched for more information on this course, but I didn't find anything good and understandable.. Tell me, how to implement this for our region, and in general? Thanks.

After the actions done, according to the advice of other participants, it turned out like this:

<?php
$Link = mysql_connect('localhost', 'root', '12345');

if(!$Link) echo "Не удалось подключится к серверу";
 else
{
   mysql_select_db('TestBD');

   $sql = "CREATE TABLE  `tests` (`id` INT NOT NULL ,`test` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , PRIMARY KEY (  `id` ))";

if (mysql_query($sql))
    echo "Создание таблицы завершено";
  else
    echo "Таблицу создать не удалось";
}
?>

But still nothing works, and it doesn't create a table...

6 answers

CREATE TABLE  `testdb`.`tests` (`id` INT NOT NULL ,`test` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , PRIMARY KEY (  `id` ));

For example, in the usual utf-8 encoding

 8
Author: alexeych, 2011-01-13 19:14:34

It is better not to use cp1251, utf is more convenient. Replace cp1251 with utf8 and cp1251_ukrainian_ci with utf8_general_ci.

 5
Author: Ama, 2012-08-24 16:14:10
<?php
   $Link = mysql_connect('localhost', 'root', '123456');

   if(!$Link) echo "Не удалось подключится к серверу";
   else
   {
      mysql_select_db('Usersbd');

      $sql = "CREATE TABLE `Userbd`.`Users` (`id` INT( 3 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,`name` VARCHAR( 25 ) CHARACTER SET

      utf8 COLLATE utf8_general_ci NULL ,`passwd` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL) ENGINE = MYISAM";
      if (mysql_query($sql))
         echo "Создание таблицы завершено";
      else
         echo "таблица не создана";
   }
?>
 3
Author: krol005, 2011-01-21 13:17:51

First, print an error, most likely an error in the syntax.

if (mysql_query($sql))
    echo "Создание таблицы завершено";
  else
    echo mysql_error();

Or even so:

mysql_query($sql) or die(mysql_error());

And about phpMyAdmin, the people correctly say, install it. Almost all database programmers use constructors.

 1
Author: Павел Владимиров, 2012-08-24 17:17:02

There is a very excellent solution to all database problems. Download phpMyAdmin you fill in your server, having previously entered the data for accessing the database into the config.default file, and you roll through all your tables like cheese in butter. And if you need the text of the database access, when creating a table using phpMyAdmin you can always view it, copy it, and use it as you please. Also a huge plus phpMyAdmin - what you can always do without problems make a backup of the entire database, and transfer it to any machine.

 0
Author: Артем Дозоров, 2011-01-29 16:04:39
sql = "CREATE TABLE 'Users'  ('UserID'  VARCHAR(5) CHARACTER SET cp1251 COLLATE cp1251_ukrainian_ci NOT NULL ". 
    " 'Name'  VARCHAR(25) CHARACTER SET cp1251 COLLATE cp1251_ukrainian_ci, ". 
    " 'E-mail'  VARCHAR(25) CHARACTER SET cp1251 COLLATE cp1251_ukrainian_ci)";
$result_query = mysql_query($sql); // теперь будет выполняться
if ($result_query) { // условный оператор проверяет выполнилось ли
    echo "Создание таблицы завершено";
} else { echo "Таблицу создать не удалось"; }
 0
Author: Артур, 2017-04-21 17:30:13