Important alert: (current site time 7/15/2013 11:24:39 AM EDT)
 

VB icon

Easy To Understand MySQL Connection

Email
Submitted on: 5/8/2001 10:15:30 PM
By: Todd Williams  
Level: Beginner
User Rating: By 12 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 29823
(About the author)
 
     This is to teach you how to connect to your mysql database and select a table. I have made it very clean and added comments to help you understand the process.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Easy To Understand MySQL Connection
//**************************************
OPEN SOURCE, but Credit would be nice. :)
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Easy To Understand MySQL Connection
// Description:This is to teach you how to connect to your mysql database and select a table. I have made it very clean and added comments to help you understand the process.
// By: Todd Williams
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=315&lngWId=8//for details.//**************************************

<?php
$server= ""; //ie: mysql.server.net
$username = ""; //ie: jonnhy
$password = ""; //ie: password101
$databasename = ""; //ie: jonnydata
$tablename= ""; //ie: emails
$connection = mysql_connect("$server","$username","$password");
if(!$connection)// are we not connected?
{
echo "Couldn't make a connection!!!";
exit; //exits the script
}
$db = mysql_select_db("$databasename",$connection);
if(!$db) //was the database not found?
{
echo "The database disapeared!";
mysql_close($connection); //closes connection
exit; // exits the code
}
echo "If you can see this you are connected to your database and your table is selected.";


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/21/2002 4:50:54 AMAhmet HIZLI

Hi,
I cannot connect to mysql server by using $connection = mysql_connect("$server","$username","$password");
my error is Fatal error: Call to undefined function: mysql_connect() in /var/www/html/ahizli/1.php on line 7 Not: Line 7 is $connection = mysql_connect("$server","$username","$password");
Please cen you help me.
Thanks.
(If this comment was disrespectful, please report it.)

 
8/23/2002 10:51:19 PMWaldner

where is the ?> for you code?
(If this comment was disrespectful, please report it.)

 
12/10/2002 3:12:10 AM

Dear Ahmet HIZLI, it sounds like you have not managed to install mySQL properly, try reinstalling it
(If this comment was disrespectful, please report it.)

 
1/2/2003 7:11:30 PM

I didn't find the variable $tablename very useful ;)
(If this comment was disrespectful, please report it.)

 
3/29/2004 4:33:15 PMKarl Bonitz

If you do not understand this code, then well I w\feel very sorry for you... j/k :)
(If this comment was disrespectful, please report it.)

 
4/20/2004 10:00:29 PM

understandable
(If this comment was disrespectful, please report it.)

 
9/17/2004 6:45:27 PMChris

Works good, basic, it's all you need.
good work
(If this comment was disrespectful, please report it.)

 
10/4/2005 10:09:37 AMxiong

Need to show how to select the table emails, and perhaps a sample echoing/printing a few columns from emails table would be helpful. I tried to add such but comment can only take 1000 chars!
(If this comment was disrespectful, please report it.)

 
7/18/2006 9:59:12 PMliesen

i was already type the code.. but the error : Access denied for user: 'root@avatar'
what wrong with my code?? any suggest??
(If this comment was disrespectful, please report it.)

 
12/6/2006 8:20:50 AMMunna

This is simple and same server connect system. But I want to mysql connect other database.

Here my code: Connection successful But a problem, not show my data. Also mysql_connect>>>
progli@progli.awardspace.com
here my data show 'tt' directly in the same server/host. Please solve and mail me: munnabd_02@yahoo.com
//if you view tt so it is true otherwise a problem.please solve it and mail me(munnabd_02@yahoo.com)
$cnn=@mssql_pconnect('db1.awardspace.com', 'progli_progli', '8013731')
or die ("Cannot connect to MySQL server: " . mysql_error());
mysql_select_db('progli_progli')
or die ("Cannot connect to db: " . mysql_error());
$result=mysql_query("select *from user");
$row=mysql_fetch_row($result);
echo $row[1];
?>

(If this comment was disrespectful, please report it.)

 
1/3/2011 3:58:13 AMmie

...helo, if it is ok pls send me also the very basic codes for add, edit, search, delete using php w/ mysql....pls....tnx..... Ill be very grateful...tnx
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.