【发布时间】:2010-01-08 05:17:46
【问题描述】:
我正在使用共享 Linux 托管,而我编写的一些 PHP 无法与服务器上的数据库一起使用。
编辑:
我解决了这个问题。我不小心从配置文件中删除了“数据库”连接变量。哇。我真傻。
编辑:
不工作是指我无法读取数据库。我已经搞砸了,知道数据库正在运行,并且当我修改密码变量时,我得到“拒绝访问”。此外,在 localhost 上,密码不同并且有效。
编辑 2: 我现在正在使用示例脚本。它现在显示一个空白页。至少没有错误...我将密码更改为字母数字。我没有使用“本地主机”。
编辑 3:使用正确的密码和服务器信息,我使用 mysql_error() 得到以下错误。我没有在数据库 url 中使用前导 http://。
无法通过socket '/var/lib/mysql/mysql.sock'连接本地MySQL服务器(2)
编辑 4:
这是我的连接代码。请注意nl() 和report() 是分别回显换行符和诊断消息的预定义函数。连接变量位于上面的外部文件中。我已经检查过 - 包含工作正常。
//////////////////////////////
// Connect to the database /////////////////////////////////////////
//////////////////////////////
//report - Attemting to establish connection ...
report(4);
////
//Step 1) Set connection in variable
////
$conn = mysql_connect($server, $user, $pass);
////
// Step 2) Verify the connection
////
if(!$conn){
//report - failed, see next line...
report(5);
//report - FATAL ERROR: Check database name, username and password.
report(9);
}else{
//report - done!
report(7);
}
//report - Selecting database ...
report(6);
////
// Step 3) Select the database
////
$db_select = mysql_select_db($database);
////
// Step 4) Verify successful selection
////
if(!$db_select){
//report - failed, see next line...
report(5);
//report - FATAL ERROR: Could not select database.
report(8);
}else{
//report - done!
report(7);
}
//report - Running query ...
report(10);
////
// Step 5) Create the original the query
////
$selector = " * ";
$target = "`properties`";
$condition = "1";
$sql = "SELECT " . $selector . " FROM " . $target . " WHERE " . $condition;
////
// Step 6) Check for a $_GET[] parameter
////
if($_GET['listing'] && !is_null($_GET['listing'])){ //if a listing number was supplied
$listingNum = htmlentities($_GET['listing']); //safety first - clean it from code injections
$pattern = '/\b[0-9]{1,6}\b/'; //define a range of valid, sercheable listings
if(preg_match($pattern,$listingNum) == 1){ //if the listing id is a valid one
$sql .= " AND `listing_id` =" .$listingNum . ""; //search for it in the database
}elseif($exp == 0){ //if the listing number is out of range
if($listingNum != "all"){ //check if the "all" keyword was ommitted - if it was not supplied.
//report - failure ... see below
report(5);
//report - Invalid listing ID
report(12);
//
// Invalid Listing ID...
//
}
}
}else if(!$_GET['listing']){
//report - failure ... see below
report(5);
//report - Invalid listing ID
report(12);
//
//No listing ID
//
}
if(!$sql){
//report - failed, see next line...
report(5);
//report - FATAL ERROR: Could not run query.
report(11);
//
// For some reason the query doesn't exist here. I really do wonder why.
//
}else{
//report - done!
report(7);
nl();
}
$result = mysql_query($sql); //perform the actual query
if(!$result){
echo("There's been some sort of error with the search lookup. Here are the details: \n" . mysql_error());
}
mysql_close($conn); //close the connection to the SQL server
【问题讨论】:
-
您是否尝试过 echo'n
phpinfo();以确保它开始工作?我记得如果你选择 Linux 主机,你会得到 PHP,所以它应该在那里。 -
php 中的所有其他内容都可以在服务器上运行。 PHP 工作。
-
“它不起作用”是什么意思?您是否收到任何类型的错误(如有必要,您应该尝试启用 error_reporting)?
-
我不是GoDaddy用户,但一般来说,您的帐户界面应该有一些关于管理数据库的部分,允许您创建数据库,设置密码。仔细检查以确保您拥有正确的密码。您还可以检查数据库是否与您的网络主机一起存在。