There is a script

define('DB_HOST','localhost'); define('DB_USER','root'); define('DB_PASSWORD','root'); define('DB_NAME','test'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if ($link) { mysql_query('CREATE DATABASE IF NOT EXISTS '.DB_NAME, $link); } else { die('Could not connect: ' . mysql_error()); } // Make my_db the current database $db_selected = mysql_select_db(DB_NAME, $link); if ($db_selected) { // If we couldn't, then it either doesn't exist, or we can't see it. $sql = "CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `nickname` varchar(256) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; $sql2 = "CREATE TABLE IF NOT EXISTS `categories` ( `id` int(255) NOT NULL AUTO_INCREMENT, `category_name` varchar(256) NOT NULL, `description` varchar(256) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; $result = $sql.$sql2; if(mysql_query($result, $link)){ echo "SUCCESS!"; } } mysql_close($link); 

One table is created and 2 unfortunately does not work.

  • semicolon did not try to put between the requests to create? you have at the end of the second ; but at the end of the first one there is no, in theory, uvas there is generally a line written together in the toga CHARSET=utf8CREATE - teran
  • I tried to concatenate with a space and; does not come out - v.krasnov
  • Do not use deprecated API, go to mysqli / pdo - vp_arth

2 answers 2

One request is impossible. One command (for example, containing several requests, if the connector allows, or calling the procedure to be executed) is possible.

Regarding the code in question - according to the documentation

mysql_query () sends one request (sending multiple requests is not supported) to the active server database

  • A remark about a single query, by the way, probably only applies to queries that return a set of data. After all, in general, there is no problem to send the server a command to perform several requests at once. The problem may be to return several datasets to the client. - teran
  • @teran Ummm ... sorry, what not everyone agrees with me on? Yes, using multi-statement for the connection is described in php.net/manual/ru/mysql.constants.php#mysql.client-flags - but I also said that the connector should allow. - Akina
  • I mean that. which would be a good idea to expand the response, indicating that with proper connection settings, mysql_query can execute several queries in one call. I did not quite understand your answer from the first reading. What was meant by different connectors is mysql , mysqli , PDO or just mysql settings. When reading, it seems (especially with reference to the documentation) that it is impossible to execute several instructions in one call. - teran
  • those. all the same, you can create 2 tables, you just need to use multi-statement php.net/manual/ru/mysql.constants.php#mysql.client-flags ? - v.krasnov

Solved the issue of using mysqli, the function mysqli_multi_query can send a query with the creation of an unlimited number of tables.