下面的代码成功地将我连接到我的mySQL数据库。
$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");我一直在本地主机上使用XAMPP和phpmyadmin进行实验,一切都正常工作。然而,今天我上传了我的网站到我的域名(我已经购买了一个域名和网站托管通过GoDaddy),我不断得到“失败的连接!”每当这些代码运行时。HTML/CSS工作正常,但我无法连接到mySQL。如何连接到服务器上的数据库?
发布于 2014-01-08 02:30:55
您需要在这里更改连接信息:
mysql_connect("localhost", "root", "")以包含您的GoDaddy数据库详细信息。有关您的帐户使用什么的更多信息,请与GoDaddy联系。
发布于 2014-01-08 02:37:43
您没有提到任何关于MySQL数据库托管的内容。如果您也是在godaddy中承载数据库,那么您应该能够从提到的这里信息中获得连接字符串和主机名
发布于 2014-01-08 03:08:33
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());
}
?>https://stackoverflow.com/questions/20985911
复制相似问题