How to avoid connecting to a database two times

Source: Internet
Author: User
                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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.