What should I do if php cannot find the database?

藏色散人
Release: 2023-03-04 20:32:01
Original
3961 people have browsed it

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.

What should I do if php cannot find the database?

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=&#39;salesinformation&#39;;              
  if (!$link)
  die(&#39;Failed to connect to Server&#39;.mysql_error());
  $db=mysql_select_db($database, $link);
  session_start();
  if(!$db)
  die(&#39;Failed to select Data Base &#39;.mysql_error());
  if(isset($_GET[&#39;process&#39;]))
   {
    $query = "Insert INTO `sales` (Username, Email, Price, Condition, RegisterDate) values(&#39;$_POST[Username]&#39;, &#39;$_POST[Email]&#39;,&#39;$_POST[Price]&#39;,&#39;$_POST[Condition]&#39;,&#39;$_POST[RegisteredDate]&#39;)";
    //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);
    }
   }
?>
Copy after login

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`)
);
Copy after login

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

there

The 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template