You are here:

MySQL/mysql connect

Advertisement


Question
hello

I am new to php and mysql, I have got myself a hosting account and have set up a small website. I am not bad with css and html but i want to be able to use a database. I have tried all sorts of tutorials on the internet and none seem to be understandable could you put it in plain engish for me.

Answer
This is what i use to contact

//define database details
$dbhost = 'localhost';
$dbuser = 'database_user';
$dbpass = 'database_password;
$database = 'database_you_want';

//conect to database
$link = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($database);


//close database connection
mysql_close($link);

The first chunk of code defines the details for your database. i would put these in an include file so that if you needed to change these it would be in one file and not on every page of your website.

the next chunk of code completes the database connection. this would go at the top of each page that required a database connection.

the last chunk of code is very important. This closes the database connection. you should put this on every page that opens a connection where the connection is no longer required. normally at the bottom of the page. This helps free up your database connections  

MySQL

All Answers


Answers by Expert:


Ask Experts

Volunteer


stuart gregory

Expertise

PHP html, xhtml, css coding and MySQL

Experience

My experiance is all first had as I am web developer and I have, and do build many websites all using php and mysql. This ranges from building a simple search engine to secure php login scripts. Some websites like http://www.web-application-development.info are complete database driven websites.

Organizations
I have a large number of websites which I host and maintain, such as. http://www.webreger.com http://www.universalscience.co.uk http://www.modellingelectronics.co.uk

Education/Credentials
Web application development. (Open University) BSC Honers Computing and development (Open University)

©2016 About.com. All rights reserved.