How can I create database in SQL inside PHP file - php

I already create a database in MySQL using
CREATE DATABASE register
And I set
$db= "CREATE DATABASE register";
$host="localhost";
$dbusername="root";
$dbpassword="";
$dbname= "register";
//create database connection
$conn = new mysqli($host,$dbusername,$dbpassword,$dbname);
but some error that becomes error
Warning: mysqli::__construct(): (HY000/1049): Unknown database 'register' in C:\xampp\htdocs\web\html\reg.php on line 19
How can I fix that

You don't have to specify a database:
$conn = new mysqli($host, $dbusername, $dbpassword);
You can then issue your query with $conn->real_query($db) (which is OK because you're just issuing a statement without any user data) and use $conn->select_db to make the database the active db after creating it.
Your assignment in the beginning ($db = ...) doesn't do anything else than assign the value to a variable - the query isn't ran before you actually send it to MySQL.

Related

How to stop PHP MySQLi from displaying warning message if db connection is failed? [duplicate]

This question already has an answer here:
Should we ever check for mysqli_connect() errors manually?
(1 answer)
Closed 2 years ago.
How can we stop PHP MySQLi from displaying ugly warning or error messages if the database connection is failed? We just want to display our own custom message instead.
Here's our code to make connection with db:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$mysqli = new mysqli($hostname, $username, $password, $dbname);
if($mysqli->connect_error)
{
die("Database connection failed: " . $mysqli->connect_error);
}
$mysqli->close();
?>
If the database "db_test" doesn't exist, we get the following error:
( ! ) Warning: mysqli::mysqli(): (HY000/1049): Unknown database 'db_test' in...
We just want to see this:
Database connection failed: Unknown database 'db_test'
What editing do I need to do in my above code?
Note
I know the database db_test doesn't exist. I am deliberately using wrong db just in order to see the warning or error message if any. I just want to see my custom error message if db connection failed (if it also fails in real scenario by any reason too) and not the warning generated from PHP.
EDIT
This warning message is only produced when I use OOP approach in MySQLi, and not with procedural.
A simple approach to hide the warning message produced by PHP and to show only your own custom message is to use suppress operator (#).
Change line from
$mysqli = new mysqli($hostname, $username, $password, $dbname);
to
#$mysqli = new mysqli($hostname, $username, $password, $dbname);

Why am I getting SQLSTATE [HY000] [1049] error when attempting to connect to a db that exists? [duplicate]

I am using PDO to connect to mySql database. I am not able to connect to any database that I create although I can connect to already created databases( already created by default). I am using wamp server.
<?php
try{
$dbh=new PDO("mysql:host=localhost;dbname=mydata","root","");
}catch(Exception $e){
die("ERROR: Couldn't connect. {$e->getMessage()}");
}
?>
If i substitute mydata with mysql which is previously created database in wamp server, then the code works perfectly. The only problem is with the databases that I create. I have tried giving mydata the same privileges as mysql database but it doesn't work.
It's either
a spelling error. Simply check the names again.
or the PHP code and PHPMyAdmin are connecting to different databases
The latter could happen, for example, if you have multiple database servers on your PC installed.
To get a proof, run the following query in phpmyadmin:
show databases;
And then run the same query in PHP, using either PDO:
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
$pdo = new PDO("mysql:host=$host", $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$databases = $pdo->query('show databases')->fetchAll(PDO::FETCH_COLUMN);
var_dump($databases);
or mysqli
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();
var_dump($databases);
and compare the output. It will show you that either there is a spelling error or indeed PHPMyAdmin and PHP are connected to different database servers.
Then you can check the configuration file in PHPmyAdmin to make sure it connects to the proper server

Can I connect more than one database into phpmyadmin using php?

Can we connect more than one database at a time in phpMyAdmin using PHP? And yes then how?
Here I use a variable database_name which contain database name and for connecting take a variable $database_selection and connect a single database which name is criminal_circum. If I want to connect more than one database instead of single database so,what should I do?
$database_name = "criminal_circum";
$db_handle = mysqli_connect($db_server_name, $db_user_name, $db_password);
$database_selection = mysqli_select_db($db_handle, $database_name);

Database is created but return error as unknown database

I already have created my database, and already have the pages in PHP ready to be connected to the database. My code of connection is the follow :
<?php
$host = "localhost";
$db = "clients";
$user = "root";
$pass = "";
$con = mysqli_connect($host,$user,$pass,$db) or die('Could not connect to MySQL: ' .mysqli_connect_error());
?>
When I try to execute the page with WAMP, it returns me the message :
Warning: mysqli_connect(): (HY000/1049): Unknown database 'clients'
Even the database being already created and the queries executed, it still gives me this error, as if the database was with the wrong name in the PHP or something.
Maybe is the error from not having some password from the user root or using localhost ? How can I fix this ?
I came across this error sometime ago while using MySQL database with node.js, I just deleted the database and created it again. It's funny though, but that was how I solved the error. Try creating the database again with another database name.

Setting up MySQL database for website

I am making a website that will use a MySQL database on a friend's web hosting service. It supports PHP and MySQL but I'm not sure what file I am meant to create the database and table in. I understand the syntax so I just need to know where and which files to do it in. I guess you don't put it in the website's html files because it only needs to be created once.
I have the following code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE DATABASE LeagueData";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
You don't have to create a file. You have to create the DB and structure from within MySQL (or you can load a .sql file into MySQL).
If he has phpMyAdmin use that. It's much easier.
Creating a table from within MySQL: http://dev.mysql.com/doc/refman/5.7/en/creating-tables.html
Importing an .sql file from the commandline: mysql -u username -p database_name < file.sql
Adding on to Patrick's answer:
You need to create your database first, and not through PHP. mysqli allows you to connect to a particular database (which you need to specify in your code), and then execute queries on that database.
First, create a database. You should consult your web-hosting provider on how to do this. Most web-hosts allow you to create a database through one of the CPanel modules. (You'll also have to create a "user", and assign that user to the newly created database.)
Once you have created the database, you will be able to connect to it via PHP. Your code to connect to the database should look like:
$conn = mysqli_connect($servername, $username, $password, $database_name);

Categories