This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Add doc for setting up a MySQL database Terminal and creating database connection with PHP #165
Open
ghost
wants to merge
60
commits into
phpmyadmin
Choose a base branch
from
master
base: phpmyadmin
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Conflicts: out/cloud9-user-documentation.json out/index.html out/setting_up_phpmyadmin.html recentFiles.json
Docs page on PHPMyAdmin
Fixing table on mongodb page
Update syntax_highlighting_themes.md to include `Dart` in the list of syntax highlighted languages.
Update syntax_highlighting_themes.md
c9 runs PHP version 5.5.9, but writing_a_php_app.html reads "We run PHP version 5.3.3.". I propose this sentence be changed to read "We run PHP version 5.5.9."
This line of code may prove to be more helpful to newcomers: ``` jekyll serve --host $IP --port $PORT --baseurl '' ``` --baseurl seems to be required if it is already defined in _config.yml. --port is necessary for Cloud9. --host appears to be unnecessary, but may be better to include for practice.
Couchdb
Conflicts: templates/default/toc.jade
Bitbucket doc: added note about Git cloning in previously created workspace
Created Java runner doc
Updated CloudFoundry CLI install section
Add hostname command-line flag
…to The13thDoc-patch-1
Conflicts: src/frameworks/frameworks_jekyll.md
…N-patch-1 Conflicts: out/writing_a_php_app.html
Great start! Merging.
Updated Google Analytics code
tidied up double usage of `sudo` to fix #158
Update setting_up_postgresql.md
Fixed double usage of sudo (#158)
Double sudo is needed so it was re-added
Added FAQ about SSHing into workspace
Added doc with workspace details for picking a plan
Mysql page update & example
Add howto for creating CGI apps using python
Changed FAQ about Digital Ocean whitelist
Adding license attribute to package.json
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Author: Graham A. Sutton
***** This is the original way before Cloud 9 added phpMyAdmin for setting up a MySQL database in Cloud 9 and forming a simple connection via PHP. This is for those who do not want to bother with phpMyAdmin or prefer creating and managing their databases in Terminal. *****
Original write-up was posted as answer to a Stack Overflow question which can be found here: http://stackoverflow.com/questions/19531822/php-connect-to-mysql-db-in-cloud-9/26986286#26986286
Credit to Brady Dowling for suggesting I create a pull request on this matter.
_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:
mysql
functions, you have to usemysqli
, sincemysql
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 asWelcome 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 .
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.CREATE DATABASE sample_db;
and hit Enter. You should get aQuery 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 itsample_db
.USE sample_db;
and hit Enter. This selectssample_db
from the list of databases.CREATE TABLE users (id INT(11), username VARCHAR(20));
, and hit Enter. This creates a table namedusers
with two columns:id
andusername
. 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.INSERT INTO users (id, username) VALUES (1, "graham12");
, and hit Enter. This will add the id of1
and a usernamegraham12
in the table. Since theid
column is anINT
, we do not put quotes around it.SELECT * FROM users;
, and hit Enter. This will show everything that is in theusers
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:Username, password, database name, and port #, are practically already known to you by now. I'll explain:
SHOW VARIABLES WHERE Variable_name = 'hostname';
, and hit Enter. You'll get a table that has 2 columns:Variable_name
andValue
. In theValue
column you should see something likeyourUsername-yourProjectName-XXXXXXX
, where theX
'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 inSHOW VARIABLES WHERE Variable_name = 'hostname';
. Re-read this step from the beginning if you're confused.)sample_db
or whatever you named your database;3306
. In Cloud 9, all of your projects are wired to3306
. 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 along
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:So in PHP, insert your credentials accordingly:
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.