简体   繁体   English

无法连接到 cpanel 数据库

[英]cannot connect to cpanel database

I have made an sql database in cpanel and adding some data to a table.我在 cpanel 中创建了一个 sql 数据库并将一些数据添加到表中。 I am now trying to access it and print out the data on my website, but I am not convinced I have the php correct.我现在正试图访问它并在我的网站上打印出数据,但我不相信我的 php 是正确的。 A tutorial I found to explain it is using xampp and not a real online website.我发现一个解释它的教程是使用 xampp 而不是真正的在线网站。 so far I have a database handler php file with,到目前为止,我有一个数据库处理程序 php 文件,

<?php

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

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

In my index.php file I have,在我的 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

I am not totally sure what I should be using for database server name, In cpanel there is a section with server info and it gives me the servername webhosting2004, I have tried this also which does not work.我不完全确定我应该使用什么作为数据库服务器名称,在 cpanel 中有一个带有服务器信息的部分,它给了我服务器名称 webhosting2004,我也试过这个也不起作用。

The username and password are as per the user details in mysql databases (using my cpanel accountname_name)用户名和密码根据 mysql 数据库中的用户详细信息(使用我的 cpanel 帐户名)

I get the following on the error log我在错误日志中得到以下信息

[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

You can start by displaying connection errors to determine if the connection works您可以首先显示连接错误以确定连接是否有效

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

Second.第二。 Make sure you have remote access to your database allowed.确保您允许远程访问您的数据库。 If not, allow it.如果没有,请允许。 To do this, go to RemoteMysql from cPanel and add %.% to allow users from any origin.为此,go 从 cPanel 到 RemoteMysql 并添加%.%以允许来自任何来源的用户。

Third.第三。 If you are not sure about the user for the database connection, you can always create a new MySQL user to use for your connection.如果您不确定数据库连接的用户,您可以随时创建一个新的 MySQL 用户用于您的连接。

Check your error log what kind of error is showing.检查您的错误日志显示什么样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM