无法使用 PHP 脚本连接到本地托管的 mysql [英] Can't connect to local hosted mysql using PHP script
问题描述
我正在尝试构建一个自托管的 PHP 站点,但是我无法从我的 PHP 脚本连接到数据库.
I am trying to build a self-hosted PHP site, but I am having trouble connecting to the database from my PHP script.
请注意以下事项
1).这段代码让我可以毫无问题地从我的终端连接到 MAMP,所以我知道 MySQL 正在工作等
1). This code allows me to connect to MAMP from my terminus without problem so I know MySQL is working etc
mysql --host=127.0.0.1 --port=8889 --user=root -p
2) 当我尝试在远程托管平台上构建站点时,此代码允许我从我的 php 脚本连接到删除服务器上的 MySQL,所以我知道此代码本身没有任何问题,但它不能在我的电脑上工作.
2) When I tried to build a site on a remote hosted platform, this code allowed me to connect from my php script to MySQL on the remove server, so I know there is nothing wrong with this code per se, but it didn't work on my computer.
defined('DB_SERVER') ? null : define("DB_SERVER","host");
defined('DB_USER') ? null : define("DB_USER","username");
defined('DB_PASS') ? null : define("DB_PASS","password");
defined('DB_NAME') ? null : define("DB_NAME","photo_gallery");
3 我已经能够在我的计算机上运行一个自托管的 wordpress 站点,但是我没有为此建立数据库连接(它在代码中的某个地方)所以我不知道我做错了什么.该站点可通过以下路径访问.http://localhost:8888/wordpress/ 即使它连接在端口 8889(根据 mysql)
3 I have been able to run a self-hosted wordpress site on my computer, but I didn't establish the database connection for that (it's somewhere in the code) so I don't what I'm doing wrong. That site is accessible at the following path. http://localhost:8888/wordpress/ even though it is connected at port 8889 (according to mysql)
4 MAMP 告诉我可以使用这种格式从我自己的脚本连接到数据库
4 MAMP tells me that I can connect to the database from my own scripts using this format
Host localhost
Port 8889
User root
Password root
因此,我添加了这一行
defined('DB_PORT') ? null : define("DB_PORT","8889");
到这个组,就像这样
defined('DB_SERVER') ? null : define("DB_SERVER","host");
**defined('DB_PORT') ? null : define("DB_PORT","8889");**
defined('DB_USER') ? null : define("DB_USER","username");
defined('DB_PASS') ? null : define("DB_PASS","password");
defined('DB_NAME') ? null : define("DB_NAME","photo_gallery");
但是还是不行.我在尝试测试时被告知数据库连接失败.
but it still didn't work. I'm being told database connection fails when I try to test it.
有什么想法吗?
编辑.我试图将端口放在本地主机旁边,但它不起作用.
EDIT. I tried to put the port next to the localhost but it's not working.
defined('DB_SERVER') ? null : define("DB_SERVER","localhost:8889");
defined('DB_USER') ? null : define("DB_USER","root");
defined('DB_PASS') ? null : define("DB_PASS","root");
defined('DB_NAME') ? null : define("DB_NAME","photo_gallery");
编辑.上面的代码在 config.php 中,它包含在 database.php 中,就是这个
EDIT. The above code was in the config.php which was included into the database.php which is this
<?php
require_once("config.php");
class MySQLDatabase {
private $connection;
function __construct(){
$this->open_connection();
}
public function open_connection(){
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if(!$connection){
die("Database connection failed:" . mysql_error());
} else {
$db_select = mysql_select_db(DB_NAME, $this->connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}
public function close_connection(){
if(isset($this->connection)){
mysql_close($this->connection);
unset($this->connection);
}
}
public function query($sql) {
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
public function mysql_prep( $value ) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
if( $new_enough_php ) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if( $magic_quotes_active ) { $value = stripslashes( $value ); }
$value = mysql_real_escape_string( $value );
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if( !$magic_quotes_active ) { $value = addslashes( $value ); }
// if magic quotes are active, then the slashes already exist
}
return $value;
}
private function confirm_query($result){
if(!$result){
die("Database query failed:" . mysql_error());
}
}
}
$database = new MySQLDatabase();
?>
推荐答案
以下代码段的输出是什么?
What is the output of the following snippet?
<?php
// test.php
$host = 'localhost:8889';
$username = 'root';
$password = 'root';
$database = 'your_database';
if(!$connection = mysql_connect($host, $username, $password))
die('Error connecting to '.$host.'. '.mysql_error());
if(!mysql_select_db($database))
die('Error selecting '.$database.'. '.mysql_error());
echo 'Connection successful.';
?>
如果执行没有错误,那么您可能只是错误地设置了常量.请注意,mysql_connect
将端口作为主机参数的一部分,而不是作为单独的参数.
If this executes without an error, then you're probably just setting your constants incorrectly. Note that mysql_connect
takes the port as part of the host parameter, not as a separate argument.
这篇关于无法使用 PHP 脚本连接到本地托管的 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!