php 无法建立到服务器的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5958119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A link to the server could not be established
提问by Michiel
A very simple insert function, and yet. It gives some nasty errors...
一个非常简单的插入函数,然而。它给出了一些令人讨厌的错误......
Like:
喜欢:
Warning: mysql_query(): Access denied for user '***.'@'***.one.com' (using password: NO) in /customers/***.be/***.be/httpd.www/belastingen/classes/btw.php on line 24 Warning: mysql_query(): A link to the server could not be established in /customers/***.be/***.be/httpd.www/belastingen/classes/btw.php on line 24
And this is the code:
这是代码:
<?php
if(isset($_POST['submit'])){
$naam = $_POST['name'];
$email = $_POST['email'];
$kind1 = $_POST['kind1'];
$kind2 = $_POST['kind2'];
$kind3 = $_POST['kind3'];
$kind4 = $_POST['kind4'];
$kind5 = $_POST['kind5'];
$captcha = $_POST['captcha'];
if ($captcha == 2){
if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['kind1'])) {
$insert = "INSERT INTO belastingen (ouder, email, kind1, kind2, kind3, kind4, kind5) VALUES (
'".$naam."',
'".$email."',
'".$kind1."',
'".$kind2."',
'".$kind3."',
'".$kind4."',
'".$kind5."')";
if (!mysql_query($insert)) {
echo "<div class=\"feedback\">query invoeren faalt</div>";
} else {
echo "<div class=\"feedback\">Uw registratie werd goed geregistreerd</div>";
}
} else {
echo "<div class=\"feedback\">falen, niveau 2</div>";
}
} else {
echo "<div class=\"feedback\">captcha probleem</div>";
}
}
?>
And don't worry about the MySQL-injection. Adding as we speak. Any thought about the error? And yes I'm sure the data for connection to the database are correct.
并且不用担心 MySQL 注入。在我们说话时添加。有没有想过这个错误?是的,我确定连接到数据库的数据是正确的。
UPDATE 1This is my inc.php
-file, included on top of the index.php file.
更新 1这是我的inc.php
-file,包含在 index.php 文件的顶部。
<?php
define('MYSQL_HOST', '***.be.mysql');
define('MYSQL_DB', '***');
define('MYSQL_USER', '***');
define('MYSQL_PASSW', '***');
require_once 'classes/dbconnections.php';
require_once 'classes/btw.php';
$_DB = new DBConnection(MYSQL_HOST, MYSQL_DB, MYSQL_USER, MYSQL_PASSW);
?>
UPDATE 2This is my dbconnections.php
-file
更新 2这是我的dbconnections.php
文件
<?php
class DBConnection {
public $host;
public $db;
public $user;
public $password;
private $_connection;
public function __construct($host = null, $db = null, $user = null, $password = null) {
$this->host = $host;
$this->db = $db;
$this->user = $user;
$this->password = $password;
$this->connect();
}
private function connect(){
$this->_connection = mysql_connect($this->host, $this->user, $this->password);
if(!$this->_connection) {
die("An error occured---- while connecting to the database: ".mysql_errno()." - ".mysql_error());
} else{
$selected = mysql_select_db($this->db, $this->_connection);
if(!$selected) {
die("An error occured while connecting to the database: ".mysql_errno()." - ".mysql_error());
}
}
}
public function listing($sql) {
$result = mysql_query($sql, $this->_connection);
while($row=mysql_fetch_array($result)) {
$return[] = $row;
}
return $return;
}
public function select($sql) {
$result = mysql_query($sql, $this->_connection);
return mysql_fetch_array($result);
}
public function insert($sql) {
mysql_query($sql, $this->_connection);
return mysql_affected_rows($this->_connection);
}
public function delete($sql) {
mysql_query($sql, $this->_connection);
return mysql_affected_rows($this->_connection);
}
public function escape($value) {
return mysql_real_escape_string($value);
}
}
?>
UPDATE 3
The error I get when replacing the thins suggested below
更新 3
更换下面建议的薄片时出现的错误
Notice: Undefined variable: _DB in /customers/***/***/httpd.www/belastingen/classes/btw.php on line 13 Fatal error: Call to a member function insert() on a non-object in /customers/***/***/httpd.www/belastingen/classes/btw.php on line 13
采纳答案by William
As per our discussion in the comments on your question, try changing things up so that inc.php is required in btw.php; btw.php is required in index.php rather than inc.php; and 'btw.php` is not required in inc.php. From reading php.net/manual/en/function.include.php, I think this might have to do with scope.
根据我们在对您的问题的评论中的讨论,尝试更改内容,以便 btw.php 中需要 inc.php;btw.php 在 index.php 而不是 inc.php 中是必需的;inc.php 中不需要'btw.php`。从阅读 php.net/manual/en/function.include.php 来看,我认为这可能与范围有关。
EDIT:
编辑:
First off, the setup you have creates a custom database object class (DBConnection
) which is used to interface with the database and execute queries. When you used mysql_query
by itself, it did not have a database connection identifier from which to execute the query, since the DBConnection object abstracts that functionality in object methods. That is why you needed to use if (!$_DB->insert($insert))
.
首先,您的设置创建了一个自定义数据库对象类 ( DBConnection
),用于与数据库交互并执行查询。当您单独使用mysql_query
它时,它没有从中执行查询的数据库连接标识符,因为 DBConnection 对象在对象方法中抽象了该功能。这就是为什么您需要使用if (!$_DB->insert($insert))
.
Secondly, and I'm not 100% on this, but essentially core the problem seems to have to do with the code in btw.php
not "seeing" the database setup code. This could have been because of two things. First, the $_DB
variable was defined afterthe btw.php
code was required, and as a result, when the PHP interpreter parsed btw.php
, $_DB
had not yet been defined. The order if the requires and the database object definition matter. Secondly, and this is where I am a bit unsure, but I think there is a variable scope/access issue when requiring btw.php
from within inc.php
(where $_DB
is defined) rather than requiring the database setup within btw.php
. In other words, you had the code that uses the database object required in the script that defines it, rather than the database setup script (including the database object declaration) required within the code that uses it.
其次,我不是 100% 对此,但基本上核心问题似乎与btw.php
没有“看到”数据库设置代码的代码有关。这可能是因为两件事。首先,$_DB
变量是在btw.php
需要代码之后定义的,结果在PHP解释器解析的时候btw.php
,$_DB
还没有被定义。需要的顺序和数据库对象定义很重要。其次,这是我有点不确定的地方,但我认为当btw.php
从内部inc.php
($_DB
定义在哪里)而不是要求在其中设置数据库时,存在可变范围/访问问题btw.php
. 换句话说,您拥有使用定义它的脚本中所需的数据库对象的代码,而不是使用它的代码中所需的数据库设置脚本(包括数据库对象声明)。
I hope that makes sense, please let me know if it is still confusing. I tend to have a problem explaining things concisely.
我希望这是有道理的,如果它仍然令人困惑,请告诉我。我倾向于在简明扼要地解释事物时遇到问题。
回答by Steve Mallory
It doesn't seem that you are using your DBConnection class to make the query. Of course, it's in that class that the connection is made. By calling mysql_query() directly, PHP is using localhost, the web server account and no password when attempting the query. So you need to do something like
您似乎没有使用 DBConnection 类进行查询。当然,连接是在那个类中建立的。通过直接调用 mysql_query(),PHP 在尝试查询时使用 localhost、Web 服务器帐户并且没有密码。所以你需要做类似的事情
if($_DB->insert($insert) > 0)
{
...
回答by ACHTEN TECHNOLOGIES
just make sure the sql query is correct this error can happen when mixup mixedup mysql and mysql query in files associated with this action or may be the erroe will be that of the parameter supplied for mysqliquery
只要确保 sql 查询是正确的,当在与此操作关联的文件中混合 mysql 和 mysql 查询时可能会发生此错误,或者可能是为 mysqliquery 提供的参数的错误