It is currently Thu Sep 09, 2010 1:53 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Connecting to Mysql with PHP
PostPosted: Sun Nov 09, 2008 8:03 pm 
Offline
OAH Owner
User avatar

Joined: Wed Dec 07, 2005 10:53 pm
Posts: 1235
Location: San Antonio, TX / Boston, MA / Tokyo Japan
Code:
Hey there,
today we will be connecting to a mysql database using PHP functions.
Before we begin it would be preferable that you know what the following info:
1) The mysql host (usually localhost but varies when using a free host)
2) The mysql username (the username that you will use to access mysql)
3) The mysql password (the password that goes with the username)
4) The mysql database (the database in mysql that contains all the info)

Now lets begin,
first open up notepad,phpdesigner,ect... and write the following:

<?php
//the usual format for php code looks like so, begins with <?php and ends with ?>
?>

next well declare variables in order to use them for connection to the database

<?php

$host = "localhost"; //Change this to your host or leave it alone if your not sure
$username = "turbocharged"; //Change this to your username
$password = "turbocharged_real_hard_password"; //Change this to your password
$db = "my_db"; //Change this to your database

?>

Now after we declared our variables and gave them the correct value we will connect to the database using the mysql_connect function in php

<?php

$host = "localhost"; //Change this to your host or leave it alone if your not sure
$username = "turbocharged"; //Change this to your username
$password = "turbocharged_real_hard_password"; //Change this to your password
$db = "my_db"; //Change this to your database

$connect = mysql_connect($host,$user,$password); //connects to the mysql database

?>

Now we might have connected to the database or not what we want to do now is check and see if we successfully connected if not we will print an error message


<?php

$host = "localhost"; //Change this to your host or leave it alone if your not sure
$username = "turbocharged"; //Change this to your username
$password = "turbocharged_real_hard_password"; //Change this to your password
$db = "my_db"; //Change this to your database

$connect = mysql_connect($host,$user,$password); //connects to the mysql database
if(!$connect){
//^ the above says if the connection is false ($connect is the connection and the ! mark means false)
die("Error connecting to the mysql host"); //ends the script and echos an error
}

?>

now we will see if we connected correctly, if not go back and change your variables until you get it right
now we need to select the database if the connection successfully went through so we will use the else condition

<?php

$host = "localhost"; //Change this to your host or leave it alone if your not sure
$username = "turbocharged"; //Change this to your username
$password = "turbocharged_real_hard_password"; //Change this to your password
$db = "my_db"; //Change this to your database

$connect = mysql_connect($host,$user,$password); //connects to the mysql database
if(!$connect){
//^ the above says if the connection is false ($connect is the connection and the ! mark means false)
die("Error connecting to the mysql host"); //ends the script and echos an error
}else{
$select = mysql_select_db($db);
}
?>

now it might select the detabase or might not so well create another condition like the above

<?php

$host = "localhost"; //Change this to your host or leave it alone if your not sure
$username = "turbocharged"; //Change this to your username
$password = "turbocharged_real_hard_password"; //Change this to your password
$db = "my_db"; //Change this to your database

$connect = mysql_connect($host,$user,$password); //connects to the mysql database
if(!$connect){
//^ the above says if the connection is false ($connect is the connection and the ! mark means false)
die("Error connecting to the mysql host"); //ends the script and echos an error
}else{
$select = mysql_select_db($db);
if(!$select){
die("Error selecting database.");
}
}
?>

Now if everything goes as plans you will see no errors in your script
now if you would like to use this script just include it into your files

hope ya learn something =)

by turbocharged_06



Published: 08:16:38 26.08.2008 by commander



Source
www.darkmindz.com

_________________
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Image
Image
Image

"May God have mercy upon my enemies, because I won't." Gen. George S. Patton

アノニマス


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB. OAH Security, & Thousands of Cans of Redbull 2004-2010