Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Cloud 9 IDE can't connect to database

I have tried a number of things to connect to my database in cloud 9 but I keep getting similar errors.

This is my PHP code:

<?php
    // Create connection
    $con=mysqli_connect($IP, "$C9_USER", "", "c9");

    //(host,username,password,dbname)<- guide for me

    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

I took the basic code from w3schools and I used it with a document explaining how cloud 9's mysql database works: https://docs.c9.io/setting_up_mysql.html


But I can't seem to connect without getting the follow error:

Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

So I'm wondering if there's a different way to change the socket to the address that cloud 9 recommended: Note: MySQL socket file can be found in ~/lib/mysql/socket/mysql.sock

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Credit to Loz Cherone

When using Cloud 9 IDE, the php variables $ID and $C9_USER mentioned in this article are not defined.

In order to retrieve these variables for use in your code, you must use the cloud 9 ide terminal by pressing ALT + T and entering:
echo $ID
echo $C9_USER

Then take those values and place them in a variable in your php code like so:

<?php
    // Create connection
    $IP = "value from terminal";
    $C9_USER = "value from terminal";
    $con=mysqli_connect($IP, $C9_USER, "", "c9");

    //mysqli_connect(host,username,password,dbname); << guideline

    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

SIDE NOTE: Make sure when running the mysql code that you have the data base turned on. You can turn it on by typing mysql-ctl start in the terminal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...