簡體   English   中英

無法連接到 MySQL:用戶 'root'@'localhost' 的訪問被拒絕(使用密碼:是)

[英]Failed to connect to MySQL: Access denied for user 'root'@'localhost' (using password: YES)

有人告訴我,將這個 url https://beachbumbookworm.com/service.php輸入到他們的瀏覽器中它可以工作。 但是,當我將 url 放在我的谷歌瀏覽器中時,它將無法正常工作。

我一直在學習本網站https://codewithchris.com/iphone-app-connect-to-mysql-database/#app在本教程中的 3.2 的教程。 下面有一個 service.php 文件,我將它粘貼到我的 MacBook Air 上的文本編輯器中。 我在 phpmyadmin 中創建了一個名為位置的表,我已經為用戶名和密碼提供了適當的權限。 密碼中沒有任何字符可以保證拒絕。 我正在使用本教程使用的內容,其中包括 wordpress + Bluehost 環境。 從 bluehost 有一個我可以訪問的控制面板,在那里我訪問了 phpmyadmin 和 mysqldatabases。 service.php 文件由我編輯后,我根據文章將其上傳到 php 中。 我不確定是什么問題,請幫忙。

<?php
 
// Create connection
$con=mysqli_connect("localhost","username","password","dbname");
 
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
 
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";
 
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // If so, then create a results array and a temporary one
    // to hold the data
    $resultArray = array();
    $tempArray = array();
 
    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }
 
    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}
 
// Close connections
mysqli_close($con);
?>

這是創建的數據庫表的圖像在此處輸入圖像描述

  1. 在這種情況下使用什么瀏覽器並不重要,因為 MySQL 連接在發送響應之前在后端創建;
  2. mysqli_connect("localhost","username","password","dbname"); 代碼嘗試用您在 PhpMyAdmin 中使用的那個替換憑證;
  3. 使用您的 MacOS 終端檢查您的 MySQL 憑證。 使用這個命令: mysql -u root -p

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM