无法建立到服务器的链接 [英] A link to the server could not be established

查看:2156
本文介绍了无法建立到服务器的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常简单的插入函数,但是。它有一些令人讨厌的错误...



喜欢:

 警告:mysql_query():对/customers/***.be/***.be/httpd.www中的用户'***.'@'***.one.com'拒绝访问(使用密码:NO) /belastingen/classes/btw.php on line 24警告:mysql_query():无法在/customers/***.be/***.be/httpd.www/belastingen/classes/中创建指向服务器的链接btw.php on line 24 

这是代码:

 <?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。',
'。
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>;
}
}
?>

不要担心MySQL注入。添加我们说话。
任何想法的错误?
是的,我确定用于连接数据库的数据是正确的。



UPDATE 1

 <$ 

 c $ c><?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 2
这是我的 dbconnections.php -file

 <?php 

DBConnection {

public $ host;
public $ db;
public $ user;
public $ password;

private $ _connection;

public function __construct($ host = null,$ db = null,$ user = null,$ password = null){
$ this-> host =
$ this-> db = $ db;
$ this-> user = $ user;
$ this-> password = $ password;
$ this-> connect();
}

私有函数connect(){
$ this-> _connection = mysql_connect($ this-> host,$ this-> user,$ this-> ;密码);
if(!$ this-> _connection){
die(连接到数据库时出现错误:.mysql_errno()。 - .mysql_error());
} else {
$ selected = mysql_select_db($ this-> db,$ this-> _connection);
if(!$ selected){
die(连接到数据库时出错:.mysql_errno()。 - .mysql_error());
}
}
}

公共函数列表($ 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

替换以下建议的

 注意:未定义的变量:_DB in /customers/***/***/httpd.www /belastingen/classes/btw.php on line 13致命错误:调用成员函数insert()对非对象在/customers/***/***/httpd.www/belastingen/classes/btw.php第13行


解决方案

你的问题,尝试改变事情,使inc.php是必需的btw.php; btw.php在index.php而不是inc.php中是必需的;和'btw.php'在inc.php中不是必需的。从阅读php.net/manual/en/function.include.php,我认为这可能与范围。



编辑:



首先,您已经创建了一个自定义数据库对象类( DBConnection ),用于与数据库连接并执行查询。当您使用 mysql_query 本身时,它没有数据库连接标识符,从中执行查询,因为DBConnection对象提取对象方法中的功能。这就是为什么你需要使用 if(!$ _ DB-> insert($ insert))



其次,我不是100%对此,但本质上核心的问题似乎与在 btw.php 的代码没有看到数据库设置代码。这可能是因为两件事。首先,在之后定义 $ _ DB 变量,需要 btw.php 因此,当PHP解释器解析 btw.php $ _ DB 尚未定义。如果require和数据库对象定义很重要的顺序。其次,这是我有点不确定,但我认为有一个变量范围/访问问题时,需要 btw.php inc .php (其中 $ _ DB 已定义),而不需要在 btw.php 。换句话说,你的代码使用了定义它的脚本所需的数据库对象,而不是使用它的代码中所需的数据库设置脚本(包括数据库对象声明)。



我希望有意义,请让我知道如果它仍然令人困惑。我倾向于有一个问题简明地解释的东西。


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.

UPDATE 1 This is my inc.php-file, included on top of the index.php file.

<?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 2 This is my dbconnections.php-file

<?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

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

解决方案

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.

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)).

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 after the 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.

I hope that makes sense, please let me know if it is still confusing. I tend to have a problem explaining things concisely.

这篇关于无法建立到服务器的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆