Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Connect to mysql server

Posted on 2004-08-26
3
Medium Priority
?
212 Views
Last Modified: 2008-03-06
Hi-

I am very, very new to php, and had a couple questions.

I want to connect to a mysql server, so I do this using mysql_connect and this handy function I found online

<?php
$user = "user";
$pass = "pass";
$db = "db";
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Couldn't connect to MySQL" );
print "Successfully connected to server<P>";
mysql_select_db( $db )
or die ( "Couldn't open $db: ".mysql_error() );
print "Successfully selected database \"$db\"<P>";
mysql_close( $link );
?>

Now it doesn't work right away, I would like to know how to point to the server, I don't believe it is localhost, do I just ask my server admin, or is there some secret?
0
Comment
Question by:Eduski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 300 total points
ID: 11907905
mysql_connect() establishes a connection to a MySQL server.
if your MySQL server is not in your localhost, just replace 'localhost' in your code with the corresponding server address.

If you got any errors, let us know.
0
 
LVL 2

Assisted Solution

by:Rajkumar_G
Rajkumar_G earned 200 total points
ID: 11911645
<?php
/*************************************************************/
// FileName : DBLib.php
/*************************************************************/

/* Configuration parameters for DB connectivity*/
$db_name = '';  //give your database name which you want to connect
$db_user = 'root';
$db_pass = '';  
$db_host = 'localhost';  //host is localhost only .If  you have apache in your system.
/*************************************************************/

global $db_handle;

/* This is where i open the floodgates to the DB */

if(!initialize($db_host,$db_user,$db_pass,$db_name))
{
        die('The database cannot be opened - Please check the configurations');
}
else
 {
   echo "database initialised";
 }

//purpose : to open the connection to database

function initialize($db_host,$db_user,$db_pass,$db_name)
{
        global $db_handle;
        if(!($db_handle = mysql_pconnect($db_host,$db_user,$db_pass)))
        {
                return false;
        }
        else
        {
                if(!mysql_select_db($db_name,$db_handle))
                {
                        return false;
                }
        }
        return true;
}

?>

 This function is used to connect to  Mysql databse and open database.
You can create a this file with the given filename and caninclude in any of your files where u need to connect this particular database in mysql.

0
 
LVL 2

Expert Comment

by:Rajkumar_G
ID: 11911652
Mysql database should reside in your system if you want to use host as localhost.
Or else give the corresponding server address in host name.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question