Connect to mysql server

Posted on 2004-08-26
Last Modified: 2008-03-06

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

$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?
Question by:Eduski
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
LVL 32

Accepted Solution

ldbkutty earned 75 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.

Assisted Solution

Rajkumar_G earned 50 total points
ID: 11911645
// 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 */

        die('The database cannot be opened - Please check the configurations');
   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;
                        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.


Expert Comment

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.

Featured Post

Technology Partners: 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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