Hongmu Notes
Home Language Notes After establishing a PHP connection to the root directory, create a database.
Language Notes PHP PHP and mysql

After establishing a PHP connection to the root directory, create a database.

After establishing a PHP connection to the root directory, create a database.

Connecting to the root user in PHP can be achieved by using the mysqli or PDO extensions.

Use the mysqli extension:

<?php
$servername = "localhost";
$username = "root";
$password = "root";

// 创建连接
$conn = mysqli_connect($servername, $username, $password);

// 检查连接是否成功
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}

// 创建数据库
$sql = "CREATE DATABASE myDB";
if (mysqli_query($conn, $sql)) {
    echo "数据库创建成功";
} else {
    echo "创建数据库时出错: " . mysqli_error($conn);
}

// 关闭连接
mysqli_close($conn);
?>

Use the PDO extension:

<?php
$servername = "localhost";
$username = "root";
$password = "root";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // 设置 PDO 错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功";
    
    // 创建数据库
    $sql = "CREATE DATABASE myDB";
    // 使用 exec() 方法执行 SQL 语句
    $conn->exec($sql);
    echo "数据库创建成功";
} catch(PDOException $e) {
    echo "连接失败: " . $e->getMessage();
}

// 关闭连接
$conn = null;
?>

In the above example, suppose you want to create a database named "myDB". When connecting as the root user, you can omit the database name and then use the "CREATE DATABASE" command in an SQL statement to create the database.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 145

Recommended reading

Specialty Snack Shop Franchising Website Template 0171

Specialty Snack Shop Franchising Website Template 0171 Practical Collection Yiyou template

This EyouCMS template is ideal for specialty snack shops and franchise businesses. Its modern and appetizing design effectively showcases the specialty snacks, store branding, franchise policies, and brand story, helping snack chain brands attract franchisees and consumers online. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 57
(Adaptive Mobile Version) PBootCMS template for mechanical manufacturing websites; Download source code for blue industrial machinery and equipment websites – 0553

(Adaptive Mobile Version) PBootCMS template for mechanical manufacturing websites; Download source code for blue industrial machinery and equipment websites – 0553 Practical Collection pbootcms Template

A PbootCMS Blue template for mechanical manufacturing and industrial machinery equipment, supporting mobile access. The professional industrial design style is ideal for machinery manufacturing companies to showcase their product ranges. It helps mechanical companies build an online brand presence and attract industrial customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 50
Responsive brand fashion women's footwear website template – 0987

Responsive brand fashion women's footwear website template – 0987 Practical Collection Yiyou template

This set of eyoucms responsive templates is suitable for the brand fashion women's shoes industry. The design style is fashionable and exquisite, and can display women's shoe products, brand image, new product releases and investment promotion. It helps women's shoe brands attract consumers and agents online and enhance brand awareness. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Yiyou CMS installation FAQ summary Yiyou...
👁 40
In a CDN, which is better: origin authentication or timestamp-based content防盗linking?

In a CDN, which is better: origin authentication or timestamp-based content防盗linking? Summary of pitfalls

Origin-based authentication and timestamp-based anti-aliasing are both commonly used anti-aliasing methods, each with its own advantages and disadvantages; the appropriate approach should be selected based on the specific circumstances. Origin-based authentication involves encrypting and signing the request URL, enabling the server to decrypt and verify the request upon receipt to determine whether resource access is permitted. Its advantage lies in its effectiveness in preventing unauthorized linking: even if an unauthorized request knows the resource's URL, it cannot pass the signature verification, thereby preventing access to that resource. However...
👁 252
Dental Cosmetic Surgery Company Website Template 0172

Dental Cosmetic Surgery Company Website Template 0172 Practical Collection Yiyou template

An eyouCMS website template designed for dental aesthetics and healthcare enterprises. Its professional and clean design is ideal for showcasing dental aesthetic treatments, medical teams, diagnostic and treatment equipment, as well as successful case studies. This template helps dental clinics attract patients online and enhance their brand professionalism. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YououCMS | YououCMS...
👁 45