无法连接到cpanel数据库

我在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 accountname_name)

我在错误日志上得到了以下内容

[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
jzh5939 回答:无法连接到cpanel数据库

您可以首先显示连接错误以确定连接是否有效

$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;
}

第二。确保您具有对数据库的远程访问权限。 如果没有,请允许。为此,请从cPanel转到RemoteMysql并添加%.%以允许来自任何来源的用户。

第三。如果不确定数据库连接的用户,则可以随时创建一个新的MySQL用户以用于连接。

本文链接:https://www.f2er.com/3159724.html

大家都在问