?
Solved

connect to an external mysql database

Posted on 2012-09-17
5
Medium Priority
?
455 Views
Last Modified: 2012-09-18
With Ms Sql a connection string lets me connect to and external ms sql database in asp.

Is there an equivalent forn an external mysql database in php?
0
Comment
Question by:robrodp
5 Comments
 
LVL 8

Assisted Solution

by:Ahmed Merghani
Ahmed Merghani earned 800 total points
ID: 38408523
When you connect to mysql DB from PHP you need to specify 4 things:
host
db name
username
password

You can use host to select external DB but ensure the external host is accessible by the local one and there is no any firewall exclude it.
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 total points
ID: 38408524
There are 3 different PHP drivers for MySQL.  http://us3.php.net/manual/en/mysql.php
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 800 total points
ID: 38408972
Most shared hosting companies have separate machines for the data base engines, so the answer to your question is, "Yes."  Your hosting company will be able to tell you the three variables you need to use to connect.  The fourth variable, DB name, is not necessary and can be specified as part of the query string.  However in practice most of us will write initialization code to connect and select the DB, then use the same DB throughout the scripts.
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 38409253
Here is a small example of what you need to have ion your php code.

<?php
mysql_connect('server_name_here', 'mysql_user_name', 'mysql_password');
// now you are connected to the host server (if username and password match)
mysql_select_db('database_name_here');
// now you can begin doing queries on that database.
?>
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38409297
Thanks for the points.  An example of MySQL connect-and-select with error visualization.
<?php // RAY_mysql_example.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);


// THE ABSOLUTE MINIMUM YOU MUST UNDERSTAND TO USE PHP AND MYSQL
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
// MAN PAGE: http://php.net/manual/en/function.mysql-query.php
// MAN PAGE: http://php.net/manual/en/function.mysql-errno.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php
// MAN PAGE: http://php.net/manual/en/function.mysql-num-rows.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-assoc.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-array.php
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php
// MAN PAGE: http://php.net/manual/en/function.error-log.php


// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";


// OPEN A CONNECTION TO THE DATA BASE SERVER
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $err <br/>";
}

// SELECT THE MYSQL DATA BASE
if (!mysql_select_db($db_name, $db_connection))
{
    $err = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $err <br/>";
    die('NO DATA BASE');
}
// IF THE SCRIPT GETS THIS FAR IT CAN DO QUERIES

Open in new window

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

621 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