...

View Full Version : newbie question: how do i connect to my database?



drake2851
11-12-2006, 04:58 AM
i went into my hosting control panel, created a database, and now want be able to use it. for instance i'm in the process of setting up a login system. it needs to use a database. the first step of the tutorial is to execute a command that sets up a table - how do i do this? where do i execute the command in? do i need any type of software?

site is www.bluefishwebsitedesign.com used php for the first time to setup the forms on the contact page and design proposal page :D



ty

bcarl314
11-12-2006, 05:20 AM
You'll most likely need to either use SSH to login to the computer and use mysql via the command line, or have phpMyAdmin installed and use that.

To use PHP to connect to a mysql database, check out the php.net documentation here:

http://us2.php.net/manual/en/ref.mysql.php

Or w3schools at:

http://www.w3schools.com/php/php_ref_mysql.asp

littlejones
11-12-2006, 03:19 PM
Can you already view PHP pages on your computer? If not I recommend you download and install WAMP from http://www.wampserver.com/en/download.php

That will install an APACHE server onto your local system so you can view PHP files and also PHP 5. It also includes PHPMyAdmin which you can use to create a database. In the phpmyadmin setup you can set up a username and password to access the database you have created. If you don't give anything a username and password then I think the master username would be 'root' but I'm not sure of the password, if any.

The code you would then place in you php file would be as follows


$host = "localhost";
$database = "your_databases_name";
$username = "your_username";
$password = "your_password";

mysql_connect($host,$username,$password) or die ("Could not connect to the host");
mysql_select_db($database) or die ("Could not connect to the database");

I hope that is of some help at least. :rolleyes:

drake2851
11-12-2006, 11:16 PM
Just got off work and going to give the above two replies a try..

TY for your help littlejones and bcarl

lates

drake2851
11-13-2006, 03:29 AM
man guys im kind of confused on a basic level. my domain is setup with php. i have multiple php functions working on my site, such as a form script to send a design proposal then another one for a contact page. i went into my control panel for my site and created a new database - a MySQL database named "datamain".

but this throws me; how do i add a table to that an existing database?

drake2851
11-13-2006, 03:55 AM
like

http://www.w3schools.com/sql/sql_create.asp


i'm not sure even where to start.. it tells me what the commands are, but how do i execute them? do i include them on a page in my website? ive read through several tutorials and sort of have a grasp on everything except this very first step! pls help, any is appreciated :D

musher
11-13-2006, 05:42 AM
like

http://www.w3schools.com/sql/sql_create.asp


i'm not sure even where to start.. it tells me what the commands are, but how do i execute them? do i include them on a page in my website? ive read through several tutorials and sort of have a grasp on everything except this very first step! pls help, any is appreciated :D

drake, the easest way to work with tables (imho) is using PHPMyAdmin easy to install and use it has an area were you can execute sql commands or you can use the interface to setup tables and such. Check out http://www.phpmyadmin.net/home_page/index.php
to down load a copy. you will need to install this on your host to work with your Mysql tables on your host (your server may provide this or another utility to manage MySQL aslo)

Side Note: I installed PHPMyAdmin on my local PC, then uploaded (FTP'd) to my host. Good idea to have MySQL and PHPMyAdmin running on your local pc for testing if thats were you do all your development work at.

littlejones
11-13-2006, 08:59 AM
But if you were going to use the database you already created then you are correct in saying that you need to create a php page with the create table statement within it. You only need to do this once so you might aswell call it "tables.php" and then discard it after use (or store it elsewhere).

So all you need to do is use the syntax I showed you above to connect to the database (the host would no longer be 'localhost' as it is on your hosts server - you'll have to figure that one out). I'm not sure if there is a default username and password if you haven't set one. My guess would be username 'root' and no password. In other words...


$host = "your_host";
$database = "your_databases_name";
$username = "root";
$password = "";

mysql_connect($host,$username,$password) or die ("Could not connect to host");

...and then the rest of your code.

I hope that help clarifies it somewhat.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum