简体   繁体   English

为什么我的mysqli_connect没有连接? 它说没有指定主机名

[英]Why isn't my mysqli_connect connecting? It says no hostname was specified when one is

I'm writing a simple blogging web app for my portfolio and I've come across this strange problem. 我正在为我的投资组合编写一个简单的博客网络应用程序,我遇到了这个奇怪的问题。 I wrote a PHP script to connect to a database and read and write data to a database by RESTful calls. 我编写了一个PHP脚本来连接数据库,并通过RESTful调用将数据读写到数据库。

I've done this before in other programs with no problems, but in this one I get an error. 我之前在其他程序中没有遇到任何问题,但是在这一次我得到了一个错误。 I wrote a simple test page to check that the RESTful calls would work before I started using them in my main app. 我写了一个简单的测试页面来检查RESTful调用在我开始在我的主应用程序中使用之前是否有效。 Instead of working, I got an error back from my PHP script. 我没有工作,而是从PHP脚本中收到错误。

The error goes like this: 错误是这样的:

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Can't connect to local MySQL server through socket '/No-MySQL-hostname-was-specified' (2) in /home/releesquirrel/storage.electricsquirrel.net/SimpleBlog/php/model_SimpleBlog.php on line 35 警告:mysqli_connect()[function.mysqli-connect] :( HY000 / 2002):无法通过套接字连接到本地MySQL服务器'/ No-MySQL-hostname-was-specified'(2)in home / releesquirrel /第35行的storage.electricsquirrel.net/SimpleBlog/php/model_SimpleBlog.php

The code leading up to line 35 goes like this: 导致第35行的代码如下:

class model_SimpleBlog {
// Properties

// Database Info
private $serverName = "mysql.electricsquirrel.net";
private $userName = "esqrl_client";
private $userPassword = "fakepassword";
private $dbaseName = "esquirrel_simpleblog";


// Methods
public function model_SimpleBlog() {
    //
}

// Load the ten latest entries after an offset
public function loadEntries($offset) {

    $result = false;

    // Connect to the Database Server

    $connection = mysqli_connect($serverName, $userName, $userPassword, $dbaseName);

I've changed the password for privacy but that's the code that's throwing the error. 我已经更改了隐私密码,但这是抛出错误的代码。 I'm completely stumped. 我完全难过了。 I've used code similar to this with no problems before, and I've tried googling the error code with no luck. 我以前使用的代码与此类似,没有任何问题,我尝试使用谷歌搜索错误代码没有运气。

Does anybody know why I'm getting this error and what I can do to fix my code? 有谁知道我为什么会收到此错误以及我可以做些什么来修复我的代码?

In PHP, you need to refer to object variables with $this-> : 在PHP中,您需要使用$this->引用对象变量:

$connection = mysqli_connect($this->serverName, $this->userName, $this->userPassword, $this->dbaseName);

$serverName , for instance, looks for a variable with that name in the scope of the function. 例如, $serverName在函数范围内查找具有该名称的变量。 Obviously none exists. 显然没有。

See the manual for more information on PHP object properties . 有关PHP对象属性的更多信息,请参见手册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM