Home > Article > Backend Development > What should I do if php cannot find the database?
The solution to the problem that php cannot find the database: first create a MySQL database named salesinformation; then import the SQL file into the database; and finally run the relevant code.
Recommended: "PHP Video Tutorial"
Specific questions:
php - Database not found?
So when I run the register.php file (which contains some basic html form to send to the database), it doesn't seem to be able to find the specific database it's looking for. All files are in the same folder and spelled correctly.
So I will paste the php code into register.php which is the most important html/css code
<?php $link=mysql_connect("localhost","root",""); $database='salesinformation'; if (!$link) die('Failed to connect to Server'.mysql_error()); $db=mysql_select_db($database, $link); session_start(); if(!$db) die('Failed to select Data Base '.mysql_error()); if(isset($_GET['process'])) { $query = "Insert INTO `sales` (Username, Email, Price, Condition, RegisterDate) values('$_POST[Username]', '$_POST[Email]','$_POST[Price]','$_POST[Condition]','$_POST[RegisteredDate]')"; //echo $query; exit; $result = mysql_query($query) or die(mysql_error()); if(!$result){ $msg = "not Inserted"; } else { $msg = "Inserted"; header("location:ClientList.php?m=".$msg); } } ?>
The database file salesinformation.sql is located in the same folder that contains this code. .
CREATE TABLE IF NOT EXISTS `sales` ( `Username` varchar(25) NOT NULL, `Email` varchar(25) NOT NULL, `Price` int(10) NOT NULL, `Condition` varchar(25) NOT NULL, `RegisterDate` date NOT NULL, PRIMARY KEY (`Username`) );
So this happens when I run xampp "http://localhost/register.php"
Failed to select database Unknown database "salesinformation"
Solution:
The database file salesinformation.sql is located in the same folder that contains this code. .
This does not mean you have a MySQL database.
1) Create a MySQL database named salesinformation.
2) Import the SQL file into the database.
3) Now you have one. Now run your code.
Can you type anything in the terminal (since I can't find any GUI for xampp when running it in ubuntu)? How to create a "MySQL" database? How can I import SQL files into it?
Use the terminal and type CREATE DATABASE salesinformation;
Use shellcd to go to the directory where the salesinformation.sql file is located.
Issue this command mysql -u root -p password salesinformation < salesinformation.sql.
Note: Since I noticed that the root password is empty in your code, you can remove the password item from the above command. If you have a password, enter
thereThe above is the detailed content of What should I do if php cannot find the database?. For more information, please follow other related articles on the PHP Chinese website!