如何在IBM Bluemix上使用php连接MySQl?

如何在IBM Bluemix上使用php连接MySQl?

问题描述:

I am install service MySQL to my PHP app on Bluemix and the error is on connection establish on this lines:

$con = mysql_connect("192.155.247.248:3307","uqDqUZ2EKoZ5I","pWXeBZbNtdpOv"); 
if (!$con){ 
    echo "Failed to connect to MySQL: " .mysql_error(); 
} 
mysql_select_db("d65a2b7e14b594d18a049ac918a4a8603",$con);

我安装服务MySQL到我在Bluemix上的PHP应用程序,错误是在连接上建立这一行: p>

  $ con = mysql_connect(“192.155.247.248:3307”,“uqDqUZ2EKoZ5I”,“pWXeBZbNtdpOv”);  
if(!$ con){
 echo“无法连接到MySQL:”。mysql_error();  
} 
mysql_select_db(“d65a2b7e14b594d18a049ac918a4a8603”,$ con); 
  code>  pre> 
  div>

Create a folder

.bp-config/options.json in the parent folder

and add

{
    "PHP_EXTENSIONS": ["mysqli"]
}

in the options.json folder the sqli connect will work fine now

Are you getting:

ERROR 2003 (HY000): Can't connect to MySQL server on '$host' (60).

A developer may have asked a similar question on developerWorks.

The answer seemed to be as follows:

  1. Downloaded the latest PHPMyAdmin(4.1.9)

  2. Created a BlueMix application using - cf push -b https://github.com/dmikusa-pivotal/cf-php-build-pack.git ${myPhpAdminApp} . Note: the PHP build pack is used is PHP 5.4.26, which enables multi-byte support (it is different from the Heroku one in the BlueMix docs). This was necessary because the Heroku pack bundled PHP 5.3.27 which doesn't enable "multi-byte" character support by default. Multi-byte support is required to be enabled by PHPMyAdmin apparently.

  3. Added the existing MySQL service to this app. And picked the host, port, user, and password details from the VCAP_SERVICES environment variable.

  4. Copied config.sample.inc.php in the PHPMyAdmin to config.inc.php and added or modified the following lines in it based on the MySQL service VCAP_SERVICES details picked in previous step -

    $cfg['Servers'][$i]['host'] = 'host-ip-from-vcap_services';
    $cfg['Servers'][$i]['port'] = 'port-from-vcap_services';
    $cfg['Servers'][$i]['user'] = 'user-from-vcap_services';
    $cfg['Servers'][$i]['password'] = 'password-from-vcap_services';
    
  5. Pushed the updates using the above cf push ... again.

Quentin suggested to use mysqli instead of mysql_* as the latter is deprecated, i.e. try this:

 $mysqli = new mysqli("192.155.247.248:3307","uqDqUZ2EKoZ5I","pWXeBZbNtdpOv", "MYDB");
 $result = $mysqli->query("SELECT * from MYTABLE");
 $row = $result->fetch_assoc();