gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:25:50 26 4
gpt4 key购买 nike

我正在为我的作品集编写一个简单的博客网络应用程序,我遇到了这个奇怪的问题。我编写了一个 PHP 脚本来连接到数据库,并通过 RESTful 调用在数据库中读取和写入数据。

我以前在其他程序中这样做没有问题,但在这个程序中我遇到了错误。在我开始在我的主应用程序中使用它们之前,我编写了一个简单的测试页面来检查 RESTful 调用是否有效。我没有工作,而是从我的 PHP 脚本中返回错误。

错误是这样的:

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

第 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);

我已经更改了隐私密码,但这是引发错误的代码。我完全被难住了。我以前使用过与此类似的代码,但没有遇到任何问题,而且我曾尝试用谷歌搜索错误代码,但没有成功。

有人知道我为什么会收到此错误以及我可以采取什么措施来修复我的代码吗?

最佳答案

在 PHP 中,您需要使用 $this-> 来引用对象变量:

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

$serverName 在函数范围内查找具有该名称的变量。显然不存在。

有关 PHP object properties 的更多信息,请参阅手册.

关于php - 为什么我的 mysqli_connect 没有连接?它说没有指定主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20431563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com