简体   繁体   English

无法连接到PHP中的MySQL数据库

[英]Having trouble connecting to a MySQL database in php

I have a php file that holds my login details named "connect.php". 我有一个php文件,其中包含我的登录详细信息,名为“ connect.php”。

    $host = 'localhost';
    $username = 'username';
    $password = 'password';
    $database = 'database';

And I'm trying to connect with another file (html) with this as its contents: 而且我正在尝试与此内容关联的另一个文件(html):

    $a = file_get_contents("http://example.com/folder/connect.php");
    echo ($a);
    mysql_connect ("$host", "$username", "$password");  
    @mysql_select_db($database) or die("Unable to select database");  

And it keeps on giving me this error: 它不断给我这个错误:

    Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access
    denied for user '**wrong username**'@'localhost' (using password: NO) in
    /home/path/public_html/repo/path/downloads.php on line 40

Instead of 代替

$a = file_get_contents("http://example.com/folder/connect.php");
echo ($a);

You should be using require / include : 您应该使用require / include

require_once '/folder/connect.php';

Your current code still won't work using HTTP since you'd need to eval or parse the result to assign your $username and $password variables, just outputting the result of the PHP script doesn't assign any variables. 您当前的代码仍无法使用HTTP,因为您需要评估或解析结果以分配$username$password变量,仅输出PHP脚本的结果不会分配任何变量。

You shouldn't need to (or be able to) use http to download your MySQL connection information through HTTP especially with no authentication. 您不需要(或能够)使用http通过HTTP下载MySQL连接信息,尤其是无需身份验证的情况。 That is a big security issue. 那是一个很大的安全问题。 If that is your setup you should change it, so that you have a local configuration file stored outside of your web root which has your db host, username, and password in it. 如果这是您的设置,则应进行更改,以便在Web根目录之外存储一个本地配置文件,该文件中包含数据库主机,用户名和密码。

you should use include instead of file_get_contents in order to get the connect.php interpreted as a php file: 您应该使用include而不是file_get_contents ,以便将connect.php解释为php文件:

include '/path/to/connect.php'; 包括'/path/to/connect.php';

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

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