School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,419 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,508 people online right now. Registration is fast and FREE... Join Now!




Connecting to MySQL Via PHP

 
Reply to this topicStart new topic

> Connecting to MySQL Via PHP

RawrItsAdam
Group Icon



post 11 Nov, 2008 - 08:37 AM
Post #1


Hello today I will be showing you how to create a connection between a MySQL server and PHP. So lets get started!

Firstly we need to define the server settings, The server address, username and password.

CODE

$server = "localhost"; //This is going to be localhost, unless otherwise stated
$db_user = "username"; //Your MySQL username
$db_pass = "password"; //Your MySQL Password.


Next we are going to use the variables we just created to connect to the server:

CODE

$conn = mysql_connect($server,$db_user,$db_pass)
      or die ('Error Connecting to Server!');


As you can see it uses the variables to create the connection, although if there is an error when creating the connection; for example the password is mis-spelt, then the user will see the message "Error Connecting to Server!"

Now that we have created the connection we need to select a database. You do this by the following:

CODE

$db_name = "database_name";
mysql_select_db ($db_name)
      or die('Error selecting the database!');


Again I have created a variable to assign the database name to, Also you will see that if there is trouble selecting the database it will produce the error message.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 11:49PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month