Reputation:
When I try to create the users table, I get the following error message:
Error creating table: No database selected
I've tried SELECT
, USE
and creating new connections, but I can't seem to connect to the secure_login database I have created. Maybe I've been formatting it wrong, I'm unsure. I know I have not included an attempt to select a database, this is for the reason that everything I'm doing just does not seem to work.
Here is the code:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected to server successfully" . "</br>";
// CREATE SECURE LOGIN
$sql = "CREATE DATABASE IF NOT EXISTS secure_login";
if ($conn->query($sql) === TRUE) {
echo "secure_login functional" . "</br>";
//CREATE USERS TABLE;
$sql = "CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password CHAR(128) NOT NULL
)";
//MAKE ADMIN USER
$sql = "INSERT INTO users (id, username, email, password)
VALUES ('1', 'admin', '[email protected]', 'password')";
if ($conn->query($sql) === TRUE) {
echo "Table users functional" . "</br>";
}
else {
echo "Error creating table: " . $conn->error . "</br>";
}
}
else {
echo "secure_login not functional: " . $conn->error . "</br>";
}
?>
Upvotes: 2
Views: 564
Reputation: 787
This is an working example .. i have just made a new connection after database is created .. with the newly created database. May be there are other alternative ways . but this is working for me ..
<?php
$servername = "localhost";
$username = "root";
$password = "root";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected to server successfully" . "</br>";
// CREATE SECURE LOGIN
$sql = "CREATE DATABASE IF NOT EXISTS secure_login";
if ($conn->query($sql) === TRUE) {
echo "secure_login functional" . "</br>";
$dbname= "secure_login";
$newConnection = new mysqli($servername, $username, $password,$dbname);
//CREATE USERS TABLE;
$sql = "CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password CHAR(128) NOT NULL
)";
if ( $newConnection->query($sql) === TRUE) {
echo "Table users functional" . "</br>";
}
else {
echo "Error creating table: " . $newConnection->error . "</br>";
}
//MAKE ADMIN USER
$sql = "INSERT INTO users (id, username, email, password)
VALUES ('1', 'admin', '[email protected]', 'password')";
if ( $newConnection->query($sql) === TRUE) {
echo "inserted successsfully" . "</br>";
}
else {
echo "Error Inserting: " . $newConnection->error . "</br>";
}
}
else {
echo "secure_login not functional: " . $newConnection->error . "</br>";
}
?>
Output
Connected to server successfully
secure_login functional
Table users functional
inserted successsfully
Upvotes: 0
Reputation: 401
You can call before //CREATE USERS TABLE:
mysqli_select_db($conn,"secure_login");
Upvotes: 0
Reputation: 2134
I believe you simply need to add a select statement after you've created the table.
Upvotes: 0
Reputation: 46900
$sql = "CREATE DATABASE IF NOT EXISTS secure_login";
if ($conn->query($sql) === TRUE) {
// Start Using it at this point :)
You have created your new database but have not told mysqli to use this for further queries, so your SELECT query has no database to select from.
You can simply do
$conn->select_db("secure_login");
Upvotes: 3