简体   繁体   English

连接到MySQL数据库时遇到问题

[英]Having Trouble Connecting to MySQL Database

The following code successfully connects me to my mySQL database. 以下代码成功将我连接到我的mySQL数据库。

$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");

I have been experimenting on localhost using XAMPP and phpmyadmin, and everything works correctly. 我一直在使用XAMPP和phpmyadmin在localhost上进行实验,并且一切正常。 However, today I uploaded my website to my domain (I have bought a domain and web hosting through GoDaddy) and I keep getting "Failed to Connect!" 但是,今天我将我的网站上传到了我的域(我已经通过GoDaddy购买了域和虚拟主机),并且不断收到“无法连接!”的信息。 whenever this code runs. 每当此代码运行时。 The HTML/CSS work correctly, but I cannot connect to mySQL. HTML / CSS可以正常工作,但是我无法连接到mySQL。 How can I connect to my database on the server? 如何连接到服务器上的数据库?

You'll need to change your connection information here: 您需要在此处更改连接信息:

mysql_connect("localhost", "root", "")

to include your GoDaddy database details instead. 而是包含您的GoDaddy数据库详细信息。 Contact GoDaddy for more information on what to use for your account. 请与GoDaddy联系,以获取有关您帐户使用什么的更多信息。

You have not mentioned anything about MySQL database hosting. 您还没有提到有关MySQL数据库托管的任何内容。 If you are hosting the database also in godaddy, you should be able to get the connection string and host name from information mentioned here 如果您也在godaddy中托管数据库,则应该能够从此处提到的信息中获取连接字符串和主机名。

First, You need to create database in your hosting server (phpmyadmin cpanel) and supply details in your database connect file.

**Looking at your database credentials I can say that you haven't created one yet since
the username is definitely not 'root' and password will be required.**

Sample db_connect file:

    <?php
    $hostname = 'e.g: internal-ra.db-server-123';
    $username = 'e.g: serverone1234d4';
    $password = 'e.g: your_own_password';

    try {
        $pdo = new PDO("mysql:host=$hostname;dbname=database_name_here", $username, $password);
        /*** echo a message saying we have connected ***/
        }
    catch(PDOException $e){
        echo 'Exception -> ';
        var_dump($e->getMessage());
        }
    ?>

并尝试尽可能少地使用键DIE()

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

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