Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to make the Database connection in PHP

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 05-09-2009
Member
 
Join Date: Apr 2009
Posts: 91
How to make the Database connection in PHP
  

I have a complete setup of PHP4.4 and MySQL database how would I make connection with the PHP to MySQL this connection would be important point of view as would update my database or upload data to my database.

Reply With Quote
  #2  
Old 05-09-2009
Member
 
Join Date: Mar 2008
Posts: 227
Re: How to make the Database connection in PHP

MySQL is a very popular database and there are lot of tutorials available on the web about using PHP with MySQL.If you want to use a database in your application, you have to make config.php file which will contain basic database data. Here we will declare database path, username, password, database name and create connection string. Put the code below into config.php file and put it in the root folder of your project.

PHP Code:
<?php
$host 
"localhost"//database location
$user "User1"//database username
$pass "Newuser123"//database password
$db_name "practise_data"//database name

//database connection
$link mysql_connect($host$user$pass);
mysql_select_db($db_name);

//sets encoding to utf8
mysql_query("SET NAMES utf8");
?>
First 4 lines are basic database data. 2 lines below is connection string which connects to server and then mysql_select_db selects database. The last line is optional but it will suggest that data will be in utf8 format. Now we have config.php file created and saved in the root folder of our project.
Reply With Quote
  #3  
Old 05-09-2009
Member
 
Join Date: Mar 2008
Posts: 192
Re: How to make the Database connection in PHP

To connect to the database, I did the following steps:
I created my own MySQL database on my computer, using the prompt screen of mySQL (you can also do it via phpMyAdmin). I added a database called database1 (the same as the real database) and two tables, Name and Addresses. I also added some data. MySql Database without showing any detailed information about connection setting, But if you want to show the success or error message then write code in following way

$conn = mysql_connect ($host, $username, $pass);

if (! $conn) {

die (?A connection to the Server could not be established!?);

} else {

echo ?Root login?,$host,? successful.?;
Reply With Quote
  #4  
Old 05-09-2009
Member
 
Join Date: Dec 2008
Posts: 161
Re: How to make the Database connection in PHP

To include config.php into application (lets say it?s a website) simply put next line on the top of the source code of index.php. That will connect to the mySQL database with the currently defined variables. So, obviously, if $connect does that, what would $connect2 do? This will do the following.
PHP Code:
<?php
$connect2
=mysql_connect($host2,$user2,$pass2); //Connect to the database.
?>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to make the Database connection in PHP"
Thread Thread Starter Forum Replies Last Post
VPN Error 5 : No hostname exists for this connection entry. Unable to make VPN connection Xmen Networking & Security 9 14-05-2012 06:05 PM
How to make database for xcode Farren Software Development 4 27-03-2012 11:17 AM
How to make Database connection in ASP.NET Neil'o Software Development 4 16-11-2011 11:06 AM
CAR MAKE & MODEL database karv Software Development 2 07-10-2011 02:19 PM
Make PAYPAL payment and save data on database Eshita The Techie Software Development 4 18-01-2011 03:52 PM


All times are GMT +5.5. The time now is 07:41 PM.