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

Hello!

I found video tutorials on the Internet on the Apache+PHP+MySQLcourse, but there all the actions take place, as I understand it, in Ukraine, although they speak in Russian. They told 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– Cyrillic comparison order with support for Ukrainian characters
not null– field is not empty

Since I live in Russia, I don’t need Ukrainian characters. I tried to remove the comparison item, the reaction was unambiguous: “The table could not be created”…

I looked 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? Thank you.

After the actions taken, on 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 the table is not created…


Answer 1, authority 100%

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


Answer 2, authority 62%

Better not to use cp1251, utf is more convenient. Replace cp1251with utf8and cp1251_ukrainian_ciwith utf8_general_ci.


Answer 3, authority 38%

<?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 "  ";
   }
?>

Answer 4, authority 12%

Print an error first, most likely a syntax error.

if (mysql_query($sql))
    echo "  ";
  else
    echo mysql_error();

or even like this:

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

and about PhpMyAdmin, people are right, install it. Almost all database programmers use constructors.


Answer 5

There is a very great solution to all database problems.
You download phpMyAdminand upload it to your server, having previously entered the data for accessing the database into the config.default file and ride all your tables like cheese in butter. And if you need a database access text, when creating a table via phpMyAdminyou can always view it, copy it and use it as you please.
Also a huge plus of phpMyAdminis that you can always backup the entire database without any problems, and transfer it to any car.


Answer 6

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 "   "; }