在PHP / SQL中选择数据库

// sql to create table
$sql = "CREATE TABLE unpw (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
username VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL,
)";

if ($conn->query($sql) === TRUE) {
    echo "Table unpw created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

I'm new to both SQL and PHP and have been working on converting some python and C++ code to PHP with an SQL server for a project of mine, and it's throwing an error saying there

"Error creating table: No database selected" (thank you error handling).

How do I select a database?

I've tried editing it to say:

$sql = "USE myDB;
CREATE TABLE unpw (

Etc, but I get the error:

"Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE unpw ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username' at line 2"

Any help? Thanks.

Have you tried changing your $servername to this?

$servername  = "mysql:host=localhost;dbname=myDb"

add this to the top, change the details with yours

$DBServer = 'server name or IP address'; // e.g 'localhost' or '192.168.1.100'
$DBUser   = 'DB_USER';
$DBPass   = 'DB_PASSWORD';
$DBName   = 'DB_NAME';

$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);