PHP create MySQL database and data table

Source: Internet
Author: User

Use PHP to link to mysqli, after successful use, Mysqli_query () Create DATABASE and data table.

<php

$con = Mysqli_connect ("localhost", "root", "root");

if (! $con)

{

Die ("No connection succeeded". Mysqli_error ());

};

if (!mysqli_query ($con, "CREATE Database Jiangxia"))

{

Die ("No success created". Mysqli_error ());

}

mysqli_select_db ($con, "Jiangxia");

$sql = "CREATE TABLE Huangyan

(UserName Vachar (15),

Usersex Vachar (15)

)";

Mysqli_query ($con, $sql);

Mysqli_colse ($con);

?>

1,** It is important to note that if a database has been established, the code:

********

if (!mysqli_query ($con, "CREATE Database Jiangxia"))

{

Die ("No success created". Mysqli_error ());

}

*******

, which can cause subsequent statements to be unable to execute.

The workaround is to use the while instead of if:

$i = 1;
while ((!mysqli_query ($con, "CREATE Database Jiangxia")) &i)

{
Echo ("wrong");
$i = $i-1;

};

The code to be solved is a bit verbose. Perhaps we have no need to judge this at all.

The parameter positions of the 2,mysql_* and mysqli_* are reversed. This needs attention.

3,mysql_query () is itself a command, whether it is a single line or a judgment condition for a function such as if.

PHP create MySQL database and data table

Related Article

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.