如何在Azure Lunix App Service提供的PHP代码中使用Mysql连接字符串 [英] How to use Mysql Connection String inside PHP code which is served by Azure Lunix App Service

查看:61
本文介绍了如何在Azure Lunix App Service提供的PHP代码中使用Mysql连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Wordpress托管到Azure lunix应用程序服务.

I'm trying to host Wordpress to Azure lunix App service.

我为MySQL创建了一个Azure数据库,然后从连接字符串屏幕中获得了Web应用程序的连接字符串.

I created a Azure Database for MySQL, then I got the web app connection string from the connection strings screen.

连接字符串格式为

Database={your_database}; Data Source={data_source}; User Id= {user_id}; Password={your_password}

然后,我创建了一个基于Linux的应用程序服务,并将MySQL连接字符串添加到其配置中.

Then I created a Linux based app service and added the MySQL connection string to its configuration.

然后我在wp-config.php中使用此代码从PHP环境变量MYSQLCONNSTR_bridgesConnection

Then I used this code in wp-config.php to get the Database connection string form the PHP environment variable MYSQLCONNSTR_bridgesConnection

<?php

$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';

foreach ($_SERVER as $key => $value) {
    echo $key ;
 if (strpos($key, "MYSQLCONNSTR_bridgesConnection") !== 0) {
 continue;
 }


 $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
 $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
 $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
 $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
}

如果您使用的是Windows App服务,则该代码有效.但是,如果您使用的是Linux App服务,则会收到此警告,并且wordpress无法正常工作

That code works if you are using windows App service. But If you are using Linux App service you will get this warning and the wordpress won't work

警告:mysqli_real_connect():(HY000/2002):第1452行的/wordpress/wp-includes/wp-db.php中没有此类文件或目录

Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /wordpress/wp-includes/wp-db.php on line 1452

我使用此代码创建了一个php信息页面

I created a php info page using this code

<?php
phpinfo();
?>

我确定PHP环境变量已加载,但是我需要一种访问它的方法.

I'm sure that the PHP environment variable is loaded but I need a way to access it.

如何使用PHP代码访问该连接字符串?

How to access that connection string in PHP code?

推荐答案

经过很长时间的搜索,我发现唯一的方法就是使用getenv函数.

After very long search I found the only way to do that is to use getenv function.

wp-config.php中的最终代码为:

$connectstr_dbhost = '';
$connectstr_dbname = '';
$connectstr_dbusername = '';
$connectstr_dbpassword = '';

$value = getenv('MYSQLCONNSTR_bridgesConnection');

 $connectstr_dbhost = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
 $connectstr_dbname = preg_replace("/^.*Database=(.+?);.*$/", "\\1", $value);
 $connectstr_dbusername = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
 $connectstr_dbpassword = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);


// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', $connectstr_dbname);

/** MySQL database username */
define('DB_USER', $connectstr_dbusername);

/** MySQL database password */
define('DB_PASSWORD', $connectstr_dbpassword);

/** MySQL hostname : this contains the port number in this format host:port . Port is not 3306 when using this feature*/
define('DB_HOST', $connectstr_dbhost);

这篇关于如何在Azure Lunix App Service提供的PHP代码中使用Mysql连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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