簡體   English   中英

無法連接到 cpanel 數據庫

[英]cannot connect to cpanel database

我在 cpanel 中創建了一個 sql 數據庫並將一些數據添加到表中。 我現在正試圖訪問它並在我的網站上打印出數據,但我不相信我的 php 是正確的。 我發現一個解釋它的教程是使用 xampp 而不是真正的在線網站。 到目前為止,我有一個數據庫處理程序 php 文件,

<?php

$dbServername = "localhost";
$dbUsername = "username";
$dbPassword = "password";
$dbName = "databasename";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

在我的 index.php 文件中,

<?php

include_once('php/db_handler.php');

?>

//some html code

    <?php
    //Choose all fields in table
    $sql ="SELECT * users;";

    //var to put in all the results
    $results = mysqli_query($conn, $sql);

    //var to get the number of rows returned.
    $resultcheck = mysqli_num_rows($result);

    //if the number of rows returned is not empty, then cycle through all results and print the stated ones to the screen.
    if($resultcheck > 0){
        while($row = mysqli_fetch_assoc($result)) {
            echo $row['$email, $username'];
        }
    }
    ?>




  //more html code

我不完全確定我應該使用什么作為數據庫服務器名稱,在 cpanel 中有一個帶有服務器信息的部分,它給了我服務器名稱 webhosting2004,我也試過這個也不起作用。

用戶名和密碼根據 mysql 數據庫中的用戶詳細信息(使用我的 cpanel 帳戶名)

我在錯誤日志中得到以下信息

[05-Nov-2019 12:31:51 UTC] PHP Warning:  mysqli_connect(): (HY000/1045): Access denied for user 'dannyjeb_admin'@'old' (using password: YES) in /home/dannyjeb/public_html/php/db_handler.php on line 8
[05-Nov-2019 12:31:51 UTC] PHP Warning:  mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/dannyjeb/public_html/test.php on line 48
[05-Nov-2019 12:31:51 UTC] PHP Warning:  mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/dannyjeb/public_html/test.php on line 51

您可以首先顯示連接錯誤以確定連接是否有效

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
if (!$conn) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

第二。 確保您允許遠程訪問您的數據庫。 如果沒有,請允許。 為此,go 從 cPanel 到 RemoteMysql 並添加%.%以允許來自任何來源的用戶。

第三。 如果您不確定數據庫連接的用戶,您可以隨時創建一個新的 MySQL 用戶用於您的連接。

檢查您的錯誤日志顯示什么樣的錯誤。

暫無
暫無

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

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