如何修复数据库失败的连接

时间:2019-05-15 07:54:03

标签: php

通过xampp运行代码时,我始终遇到此错误。即使启动了MySQL和Apache

  

警告:mysqli_connect():(HY000 / 1049):未知数据库   第6行的C:\ xampp \ htdocs \ dbconnection.php中的'loginsystem'失败   连接到MySQL:未知数据库'loginsystem

这是我的数据库连接代码

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'loginsystem');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

// Check connection
if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

 ?>

loginsystem.sql

-- phpMyAdmin SQL Dump
-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 24, 2019 at 05:58 PM
-- Server version: 10.1.37-MariaDB
-- PHP Version: 7.3.0

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `loginsystem`
--

-- --------------------------------------------------------

--
-- Table structure for table `admin`
--

CREATE TABLE `admin` (
`id` int(11) NOT NULL,
 `username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

  --
-- Dumping data for table `admin`
--

INSERT INTO `admin` (`id`, `username`, `password`) VALUES
(1, 'admin', 'dd077f889d7e3641bf9cc0e64b157918');

 -- --------------------------------------------------------

--
-- Table structure for table `off`
--

CREATE TABLE `off` (
`id` int(11) NOT NULL,
`dateoff` date NOT NULL,
`timeoff` text NOT NULL,
`dateback` date NOT NULL,
`timeback` text NOT NULL,
`reason` text NOT NULL,
`status` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
`id` int(11) NOT NULL,
`fname` varchar(255) NOT NULL,
`lname` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(300) NOT NULL,
`contactno` varchar(11) NOT NULL,
`posting_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `fname`, `lname`, `email`, `password`, 
`contactno`, `posting_date`) VALUES
(11, 'ogheneruemu', 'precious', 'ogheneruemup@gmail.com', 
'dd077f889d7e3641bf9cc0e64b157918', '09028564529', '2019-03-24');

--
-- Indexes for dumped tables
--

 --
-- Indexes for table `admin`
--
ALTER TABLE `admin`
ADD PRIMARY KEY (`id`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `admin`
--
ALTER TABLE `admin`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

我一直收到此错误。而且它不允许我登录

  

警告:mysqli_connect():(HY000 / 1049):未知数据库   第6行的C:\ xampp \ htdocs \ dbconnection.php中的'loginsystem'失败   连接到MySQL:未知数据库'loginsystem

1 个答案:

答案 0 :(得分:0)

您可能尚未在mySQL服务器中创建数据库。

您需要连接到MySQL服务器运行 CREATE DATABASE loginsystem;