0

以下代码成功地将我连接到 mySQL 数据库。

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

我一直在使用 XAMPP 和 phpmyadmin 在 localhost 上进行试验,一切正常。但是,今天我将我的网站上传到我的域(我已经通过 GoDaddy 购买了一个域和网络托管)并且我不断收到“无法连接!” 每当此代码运行时。HTML/CSS 工作正常,但我无法连接到 mySQL。如何连接到服务器上的数据库?

4

4 回答 4

1

您需要在此处更改连接信息:

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

改为包含您的 GoDaddy 数据库详细信息。请联系 GoDaddy,了解有关您的帐户使用什么的更多信息。

于 2014-01-08T02:30:55.490 回答
0

您没有提到任何有关 MySQL 数据库托管的内容。如果您也在 godaddy 中托管数据库,您应该能够从此处提到的信息中获取连接字符串和主机名

于 2014-01-08T02:37:43.543 回答
0
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());
        }
    ?>
于 2014-01-08T03:08:33.300 回答
0

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

于 2014-01-08T03:52:34.767 回答