if (!@mysql_select_db($dbname,$conn)) { if (!@mysql_query('CREATE DATABASE '.$dbname)) { exit('指定的数据库('.$dbname.')系统尝试创建失败,请通过其他方式建立数据库'); } else { @mysql_select_db($dbname,$conn); } }
The above code means if the database does not exist, then create and then connect, if the data inventory, direct connection, how to optimize the structure, thank you.
Reply content:
if (!@mysql_select_db($dbname,$conn)) { if (!@mysql_query('CREATE DATABASE '.$dbname)) { exit('指定的数据库('.$dbname.')系统尝试创建失败,请通过其他方式建立数据库'); } else { @mysql_select_db($dbname,$conn); } }
The above code means if the database does not exist, then create and then connect, if the data inventory, direct connection, how to optimize the structure, thank you.
Method One: No longer judge, direct CREATE DATABASE IF NOT EXISTS 'db_name'; .
Method Two: Query SHOW DATABASES LIKE 'db_name' . If the database does not exist, an empty set is returned.
But anyway, trying to create a database seems like a bad practice. If the average user can arbitrarily increase or decrease the number of databases, then the impact on the database system is catastrophic.
The virtual host is generally not allowed to do so, but first to use the control Panel to operate the MySQL privileged users to build the library, and then the existing library to authorize the use of ordinary users. and WP, DISCUZ and other mainstream PHP programs will not attempt to create a database.