php连接到云9中的mysql db? [英] php connect to mysql db in cloud 9?

查看:433
本文介绍了php连接到云9中的mysql db?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在我的PC中使用localhost将php连接到mysql。
现在我试图将我的项目放在云端 https://c9.io ,但我不能连接到mysql。我已经在云中有mysql数据库,并把我的项目放在同一个地方...

  mysql_connect(/ lib / mysql / socket /mysql.sock\",\"myUser,)或die(mysql_error()); 

我使用上面的脚本连接,但我得到未知的MySQL服务器主机' lib / mysql / socket / mysql.sock'(1)



我该做什么?

解决方案

好吧,所以上面的答案都没有为我工作,但幸运的是我能够设置一个数据库,并运行自己的方式,我现在可以进行查询并成功运行它们,所以我将与您分享我的方法,希望任何人在搜索互联网可以绊到这一点,而不必经历同样的头抓,我做了。



如果您想快速下载,只需滚动到第3步,然后从那里继续阅读。如果你是一个完整的初学者,请继续阅读,我将详细介绍它。



有几件事要提及:




  • 您必须通过Cloud 9中的终端设置一个数据库。我以前在终端中没有经验,但是学习非常简单。

  • 您可以使用 mysql 函数,必须使用 mysqli ,因为 mysql 函数已弃用,Cloud 9将不会运行它们。






第1步:在Cloud 9(终端)中设置MySQL



在您的项目中,打开一个新终端(单击文本编辑器空间上方的加号选项卡,选择新终端)。在终端中,键入 mysql-ctl start ,然后按Enter键。



接下来,输入 mysql-ctl cli ,然后按Enter键。您应该看到一些开头为欢迎使用MySQL监视器的文本... 。恭喜您在Cloud 9项目中设置了MySQL。

一个测试数据库(在Terminal中)



你可以继续创建你的官方数据库,但是为了这个缘故,我只需要创建一个数据库包含ID和用户名的表。下面是设置数据库和表的步骤。如果你以前使用MySQL和数据库,那么这应该是蛋糕,但我会详细解释它们可能不完全了解MySQL的人。


  1. 键入 SHOW DATABASES; ,然后按Enter键。这将显示项目中当前数据库的列表。

  2. 输入 CREATE DATABASE sample_db; 输入您的数据库列表,并按Enter键。你应该得到一个查询确定,1行受影响。这意味着查询成功。您可以为数据库命名任何你喜欢的,但对于这个小步骤,我命名为 sample_db

  3. 输入 USE sample_db; ,然后按Enter键。这将从数据库列表中选择 sample_db

  4. 输入 CREATE TABLE users ,username VARCHAR(20)); ,然后按Enter键。这将创建一个名为 users 的表,其中包含两个列: id username 。括号中的数字表示列将存储在数据库中的字符数限制。在这种情况下,例如 username 不会包含长度超过20个字符的字符串。

  5. 输入 INSERT INTO users(id,username)VALUES(1,graham12); ,然后按Enter键。这将在表中添加 1 的I​​D和用户名 graham12 。由于 id 列是 INT ,我们不会在其周围加引号。

  6. 键入 SELECT * FROM users; ,然后按Enter键。这将显示 users 表中的所有内容。






p> 第3步:获取从PHP连接到数据库所需的凭据。 (在Terminal中)



现在我们在表中有一些数据,我们可以测试我们的 mysqli 连接。但首先,我们必须得到我们将需要连接到PHP数据库的凭据。在Cloud 9中,我们需要5个凭据才能连接:


  1. 主机名

  2. li>
  3. 密码

  4. 数据库名称

  5. 端口#

用户名,密码,数据库名和端口号现在已经为您所知。我将解释:


  1. 主机名 - 输入 SHOW VARIABLES WHERE Variable_name = 'hostname'; ,然后按Enter键。您将得到一个包含两列的表: Variable_name Value 。在 Value 列中,您应该会看到 yourUsername-yourProjectName-XXXXXXX ,其中 X 是一个7位数字。写下这个数字或保存它在哪里。这是您的主机名。 (如果你在这个演练中得到快速解决,只需启动一个新的终端并启动你的mysql并选择你想要使用的数据库,然后输入 SHOW VARIABLES WHERE Variable_name ='hostname'; 。如果您感到困惑,请从头开始重新阅读此步骤。)

  2. 用户名 - 用于记录的用户名
  3. - 在Cloud 9中有数据库的
    密码。
  4. 数据库名称 - 这将是 sample_db 或您为数据库命名的任何内容;

  5. Port# - 是 3306 。在Cloud 9中,所有项目都连接到 3306 。这是Cloud 9的通用常量。它不会是其他任何东西。这样写一个整数,不是作为字符串。 mysqli_connect()会将端口#解释为 long 数据类型。






    1. 最后一步:使用PHP连接到数据库! (使用PHP)



      打开一个PHP文件,并随意命名。



      对于此示例,我的主机名为 graham12-sample_db-1234567 ,这是我的数据:




      • 主机名:graham12-sample_db-1234567

      • 用户名:graham12

      • 密码:

      • 数据库名称:sample_db

      • 端口号:3306

      • <

        因此,在 PHP 中,相应插入您的 凭证:

         <?php 

        //连接到数据库
        $ host =grahamsutt12-sample_db-1234567; //请参阅步骤3关于如何获取主机名
        $ user =grahamsutt12; // Your Cloud 9 username
        $ pass =; //记住,没有密码!
        $ db =sample_db; //您要连接到的数据库名称
        $ port = 3306; //港口 #。它总是3306

        $ connection = mysqli_connect($ host,$ user,$ pass,$ db,$ port)或die(mysql_error());



        //现在执行一个简单的查询以确保它正在工作
        $ query =SELECT * FROM users;
        $ result = mysqli_query($ connection,$ query);

        while($ row = mysqli_fetch_assoc($ result)){
        echoID is:。 $ row ['id']。 ,用户名是:。 $ row ['username'];
        }

        ?>

        如果你得到一个结果没有错误,那么你已经成功地建立了一个数据库,与PHP在云9.你现在应该能够进行所有的查询,你可以正常做。



        注意:为了简单起见,我演示了最后一部分,不使用参数化查询。在使用真正的Web应用程序时,应始终使用参数化查询。您可以在此处获取更多信息: MySQLi准备的语句


        I usually connect php to mysql with localhost in my PC.. now i'm trying to put my project in cloud https://c9.io ,but i can't connect to mysql. i already have mysql database in cloud and put my project in same place...

        mysql_connect("/lib/mysql/socket/mysql.sock","myUser","") or die(mysql_error());
        

        i use script above to connect but i get Unknown MySQL server host '/lib/mysql/socket/mysql.sock' (1)

        what shoul i do ?

        解决方案

        Okay, so none of the above answers had worked for me, but fortunately I was able to setup a database and get it up and running my own way and I can now make queries and run them successfully, so I will share my method with you in hopes that anyone else scouring the internet can stumble across this and not have to go through the same head scratching that I did.

        If you want the quick rundown, just scroll to Step 3 and read on from there. If you're a complete beginner, keep reading as I'll walk you through it in detail.

        Couple things to mention:

        • You will have to setup a database via a Terminal in Cloud 9. I had no experience prior doing it in a Terminal before, but it's very simple to learn.
        • You can not use mysql functions, you have to use mysqli, since mysql functions are deprecated and Cloud 9 will not run them.

        Step 1: Setup MySQL on Cloud 9 (in Terminal)

        In your project, open up a New Terminal (click the plus-sign tab above the text editor space, select "New Terminal"). In the terminal, type mysql-ctl start and hit Enter. MySQL will start up in the back, but you won't get any response back in the terminal.

        Next, type mysql-ctl cli and hit Enter. You should see some text that starts off as Welcome to the MySQL monitor.... Congrats, you've setup MySQL on your Cloud 9 project.


        Step 2: Create a test database (in Terminal)

        You can actually go ahead and create your official database if you like, but for this sake I'll just make a database that holds a table that holds an ID and a username. So here's the steps to setting up a database and a table. If you've used MySQL and databases before, then this should be cake, but I'll explain it in detail for those who might not fully understand MySQL .

        1. Type SHOW DATABASES; and hit Enter. This will show a list of current databases within your project. You can enter this any time you want to see a list of your databases on the current project.
        2. Type in CREATE DATABASE sample_db; and hit Enter. You should get a Query OK, 1 Row affected. which means the query was successful. You can name the database whatever you like, but for this little walk-through, I named it sample_db.
        3. Type in USE sample_db; and hit Enter. This selects sample_db from the list of databases.
        4. Type in CREATE TABLE users (id INT(11), username VARCHAR(20));, and hit Enter. This creates a table named users with two columns: id and username. The number in parentheses represents the character limit the column will store in the database. In this case for example, username won't hold a string longer than 20 characters in length.
        5. Type in INSERT INTO users (id, username) VALUES (1, "graham12");, and hit Enter. This will add the id of 1 and a username graham12 in the table. Since the id column is an INT, we do not put quotes around it.
        6. Type in SELECT * FROM users;, and hit Enter. This will show everything that is in the users table. The only entry in there should be what we inserted from the last step we just did.


        Step 3: Get the credentials you'll need to connect to the database from PHP. (in Terminal)

        Now we have some data in our table that we can test our mysqli connection with. But first, we have to get the credentials we will need to connect to the database in PHP. In Cloud 9, we will need 5 credentials to connect:

        1. Host name
        2. Username
        3. Password
        4. Database name
        5. Port #

        Username, password, database name, and port #, are practically already known to you by now. I'll explain:

        1. Host name - Type in SHOW VARIABLES WHERE Variable_name = 'hostname';, and hit Enter. You'll get a table that has 2 columns: Variable_name and Value. In the Value column you should see something like yourUsername-yourProjectName-XXXXXXX, where the X's are a 7 digit number. Write this number down or save it some where. This is your host name. (If you're getting the quick rundown on this walkthrough, just start a new terminal and start up your mysql and select the database you want to use, then type in SHOW VARIABLES WHERE Variable_name = 'hostname';. Re-read this step from the beginning if you're confused.)
        2. Username - Your username that you use to log in to Cloud 9.
        3. Password - There is NO password for your database in Cloud 9.
        4. Database name - This would be sample_db or whatever you named your database;
        5. Port # - is 3306. In Cloud 9, all of your projects are wired to 3306. This is a universal constant of Cloud 9. It will not be anything else. Write this as you would an integer, not as a string. mysqli_connect() will interpret the port # as a long data type.


        Last Step: Connect to the database with PHP! (using PHP)

        Open up a PHP file and name it whatever you like.

        I'll pretend that my host name is graham12-sample_db-1234567 for this example and that this is what my data looks like:

        • Host name: "graham12-sample_db-1234567"
        • Username: "graham12"
        • Password: ""
        • Database name: "sample_db"
        • Port #: 3306

        So in PHP, insert your credentials accordingly:

        <?php
        
            //Connect to the database
            $host = "grahamsutt12-sample_db-1234567";   //See Step 3 about how to get host name
            $user = "grahamsutt12";                     //Your Cloud 9 username
            $pass = "";                                 //Remember, there is NO password!
            $db = "sample_db";                          //Your database name you want to connect to
            $port = 3306;                               //The port #. It is always 3306
        
            $connection = mysqli_connect($host, $user, $pass, $db, $port)or die(mysql_error());
        
        
        
            //And now to perform a simple query to make sure it's working
            $query = "SELECT * FROM users";
            $result = mysqli_query($connection, $query);
        
            while ($row = mysqli_fetch_assoc($result)) {
                echo "The ID is: " . $row['id'] . " and the Username is: " . $row['username'];
            }
        
        ?>
        

        If you get a result and no error then you have successfully setup a database and formed a connection to it with PHP in Cloud 9. You should now be able to make all the queries you can normally make.

        Note: I demonstrated the last part without using parameterized queries for the sake of being simple. You should always use parameterized queries when working with real web applications. You can get more info on that here: MySQLi Prepared Statements.

        这篇关于php连接到云9中的mysql db?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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