首頁  >  文章  >  後端開發  >  php找不到資料庫怎麼辦

php找不到資料庫怎麼辦

藏色散人
藏色散人原創
2020-08-20 10:39:353896瀏覽

php找不到資料庫的解決方法:先建立一個名為salesinformation的MySQL資料庫;然後將SQL檔案匯入該資料庫;最後執行相關程式碼即可。

php找不到資料庫怎麼辦

推薦:《PHP影片教學

具體問題:

#php - 找不到資料庫?

因此,當我執行register.php檔案(其中包含一些要傳送到資料庫的基本html表單)時,它似乎找不到它要尋找的特定資料庫。所有文件都位於同一資料夾中,拼字正確。

所以我將把php程式碼貼到register.php中,這是最重要的html/css程式碼

<?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);
    }
   }
?>

資料庫檔案salessinformation.sql位於包含此程式碼的相同資料夾中。 。

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`)
);

所以當我執行xampp「http://localhost/register.php」時會發生這種情況

未能選擇資料庫未知資料庫「salesinformation」

#解決方案:

資料庫檔案salessinformation.sql位於包含此程式碼的相同資料夾中。 。

這並不代表你有一個MySQL資料庫。

1)建立一個名為salesinformation的MySQL資料庫。

2)將SQL檔案匯入該資料庫。

3)現在你有了一個。現在運行你的程式碼。

你能在終端機中輸入什麼嗎(因為在ubuntu中運行xampp時我找不到它的任何GUI)?如何建立“MySQL”資料庫?如何將SQL檔案匯入到其中?

使用終端,鍵入CREATE DATABASE salesinformation;

使用shellcd轉到salesinformation.sql檔案所在的目錄。

發出此指令mysql -u root -p password salesinformation < salesinformation.sql。

注意:由於我注意到您的程式碼中的根密碼為空,您可以從上面的命令中刪除password項目。如果你有密碼的話,在那裡輸入

以上是php找不到資料庫怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn