how to connect database in different server? - php

I am using two domain name ('domain1','domain2') and 2 server('server A' and 'server B'). 'domain1' is the Main domain. 'domain2', i am using as backend like, to store data. So the data will be stored in server B. When i use 'domain1' in browser it will display the data which is stored in 'server B' . I am not using 'server A' .
I have done some code. Please go through it and let me know what correction i have to do. The code is below,
//these hostname,dbname,user and password from server B,(is this correct?)
$hostname = "1.1.1.1" ;
$database = "db_B" ;
$user = "u_B" ;
$pasword = "u_pas" ;
$conn = mysqli_connect($hostname,$user,$pasword,$database) ;
if (!$conn)
{
die ("Error connecting to the database because : " .
mysqli_error($conn)) ;
}

In domain1 config.php file i have written the following code. It's working for me.
$servername = "domain2.in" ;
$database = "db_B" ;//server B's database name
$user = "u_B" ;//server B's database's(db_B) user name
$pasword = "u_pas" ;//server B's database's(db_B) password
$conn = mysqli_connect($servername,$user,$pasword,$database) ;
if (!$conn)
{
die ("Error connecting to the database because : " .
mysqli_error($conn)) ;
}
after this, add "server A"'s Remote MySQL hostname in "server B"'s Remote MySQL.

Related

Connecting to local Database from remote web server

I am trying to connect to my local Database from the webserver but i get
Fatal error: Call to undefined function odbc_connect()
in -/-/-/7001238/web/s/sage2.php on line 15"
Any help on how to fix issue.
Here is the code i used to connect.
$odbc['dsn'] = "Sage50";
$odbc['user'] = "Peach";
$odbc['pass'] = "XXXX";
$mysql['host'] = "localhost";
$mysql['user'] = "root";
$mysql['pass'] = "";
$mysql['dbname'] = "sagetest";
$mysql['idfield'] = "id";
$debug=true;
// Step 1: Connect to the source ODBC and target mysql database
if ($debug) echo "Connect to " . $odbc['dsn'] . ' as ' . $odbc['user'] . "\n";
$conn = odbc_connect($odbc['dsn'], $odbc['user'], $odbc['pass']);
if (!$conn) {
die("Error connecting to the ODBC database: " . odbc_errormsg());
}
$myconn = mysql_connect($mysql['host'], $mysql['user'], $mysql['pass']);
if (!$myconn)
die("Error connecting to the MySQL database: " . $mysql_error());
if (!mysql_select_db($mysql['dbname'], $myconn)) die("Error selecting the database: " . mysql_error());
// Step 1.5: loop through each table with steps 2-7
$allTables = odbc_tables($conn);
$tablesArray = array();
while (odbc_fetch_row($allTables)) {
if (odbc_result($allTables, "TABLE_TYPE") == "TABLE") {
$tablesArray[] = odbc_result($allTables, "TABLE_NAME");
}
}
Thank you for your time!
First: This error happens because you don't have the ODBC PHP extension installed.
Check http://php.net/manual/en/odbc.installation.php too.
In debian distros you can solve this with a apt-get install php5-odbc, but you can check this also with your hosting provider.
When you see a Call to undefined function you always must check the php.net to be sure about the name of function, or the extension is not loaded.
PS 1: I think you're trying to compare/transfer data between two databases, right?
PS 2: Make sure your server can reach the ODBC address. The webserver is not your dev machine, so localhost is not the real localhost ;)

Development and Production php/MySQL queries management with Mercurial

I have a web project I'm versioning with Mercurial and I don't know how to manage access to two different databases; one is for Development purposes and the other one is for Production.
For now my project is still in development, so I access the dev database and make queries on some of its tables with a php script as for example :
<?php
$dbuser = 'something';
$dbpassword = 'something';
$dbname = 'devDBName';
//~ //connect
$link = mysql_connect('servName', $dbuser, $dbpassword);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
metricsName();
function metricsName()
{
$sql = "SELECT id_metric, name_metric FROM metric";
$result = mysql_query($sql); // result set
while($rec = mysql_fetch_array($result, MYSQL_ASSOC)){
$arr[] = $rec;
};
$data = json_encode($arr); //encode the data in json format
echo '({"success": "true", "message" : "OK","data":' . $data . '})';
}
?>
But I don't know how to access a different database for the Production environment, should I make a copy of these PHP scripts and put explicitly the name of the production database? Or is there a way to "parameterize" this?
Any help would be appreciated.
Separate out your code so that the configuration (i.e. database connections, any other environment specific config / code) is kept in one place.
It makes it easier to maintain (as it's kept centrally), and your build / deployment process can deal with which configuration details need to be used.
Clearly your project is at a very early stage, but this approach will help as it grows.
This example is for mysqli, but the idea should apply to the library you are using. in a config.php file do something like:
//define('DB_HOST_NAME', 'prodserver.com');
define('DB_HOST_NAME', "devserver");
define('DB_NAME', "databasename");
define('DB_USER', "username");
define('DB_PW', "password");
you could put some code around the define statements that will check which server the code is running on and determine which database to connect to. If the dev server for the php is different from the prod server for the php.
Then in your database.php do something like
$include_path = $_SERVER['DOCUMENT_ROOT'] . "/someincludedir";
.....
require $include_path . '/config.php';
......
class myDatabase
{
var $db_host_name = DB_HOST_NAME;
var $db_name = DB_NAME;
var $db_user = DB_USER;
var $db_pw = DB_PW;
var $mysqli;
function connect()
{
$this->mysqli = new mysqli
(
$this->db_host_name,
$this->db_user,
$this->db_pw,
$this->db_name
) or die ("mysqli interface failed");
if( mysqli_connect_errno() )
{
printf("COULD NOT CONNECT TO DATABASE -- mysqli connection failed: %s\n", mysqli_connect_error());
}
}
}
Note that the above class assumes it will be a singleton and if you wanted to have multiple connections to the same or different databases you would then parameterize the database, username, etc for the connect function and you could connect to different databases with different objects.

IIS to Apache connection issue

I am moving what looks like a pretty simple php/MSQL app from a Windows IIS server to a Cpanel/ Apache server. The app works fine on existing IIS/MySQL server using this format to connect:
<?php
class Connection {
var $conn;
var $user = 'XX0000XX_XXadmXX';
var $pass = 'XXXpasswordXXXX';
var $dbname = 'XXckbXXX_XXX13XX';
var $host = '74.0.0.0';
function Connection() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->dbname,$this->conn) or die( "db-connect". mysql_error());
}
function closedb() {
//mysql_close(($this->host, $this->user, $this->pass ) or die( "db-close". mysql_error() );
mysql_close($this->conn)or die( "db-close". mysql_error() );
}
}
?>
I imported the DB into CPANEL/MySQL using PhpMyAdmin. 1st test failed due to $host not accepting IP address value. After changing IP to localhost - tested the connection with a DB connection script and that passed. The error is that at I can connect with the same username and PW as Windows server. But after logging in any link I click in the admin kicks me out.
Any clues would be helpful - let me know if I can provide any information that would help. I am thinking that there may be a windows specific change that needs to happen or something to do with the the DB host.
Here are some values in a settings file - before the Ip addresses were set with domain name - set with IP to test:
define('ADMIN_MANAGEMENT', "sitename");
define('FROM_NAME', "Admin");
define('SITE', "sitename");
define('SITE_NAME', "http://74.0.0.0/");
define('ADMIN_ID', "dwayne#fair.com");
define('KEYWORD', "sitename");
define('SITE_URL',"http://74.0.0.0/");
define('SITEIMGPATH', "http://74.0.0.0/public/images/");
define('ITEMS_PER_PAGE', "15");
define('PRODUCTS_PER_PAGE', "5");
define('UPLOAD_FILES', "uploads/files/");
define('UPLOAD_IMAGES', "uploads/images/");
define('SMALL_IMAGE_WIDTH',190);
define('SMALL_IMAGE_HEIGHT',77);
define('PROFILENAME', "Welcome to domain.com");
define('CARID',1000);
define('RESIZE_IMAGE_WIDTH',400);
define('RESIZE_IMAGE_HEIGHT',300);
/*Live path*/
define("ROOT", "http://" . $_SERVER["SERVER_NAME"] . "/admin/");
define("ROOTS", "http://" . $_SERVER["SERVER_NAME"] . "/admin/");

Php MSSQL(2012) giving error:Couldn’t connect to SQL Server

I am trying to fetch a set of fields from a Database on MSSQL Database on SQL Server 2012. This is a remote server and I am trying the following piece of code.
//MSSQL Server for retrieving the Member name from Member ID:
//mssql.secure_connection = On
// Need to upload ntwdblib.dll from net
$myServer = "IPAddress/SQLExpress "; // host/instance_name
$myUser = "ID"; // username
$myPass = "pass"; // paasword
$myDB = "dbname"; // database name
// connection to the database
$dbhandle = mssql_connect($myServer, $myUser,$myPass)
or die("Couldn’t connect to SQL Server on $myServer"). mssql_get_last_message();
// select a database to work with
$selected = mssql_select_db("sportsclub", $dbhandle)
or die("Couldn’t open database $myDB");
echo "You are connected to the " . $myDB . " database on the " . $myServer . ".";
$query = "SELECT first_name, last_name";
$query .= "FROM members ";
$query .= "WHERE member_id='".$row['member_id']."'";
// your database query
$result = mssql_query($query);
while($row = mssql_fetch_assoc($result))
{
echo "<td>" . $rows["first_name"] . $rows["last_name"] . "</td>";
}
// close the connection
mssql_close($dbhandle);
//Ended MSSQL Connection
It simply does not connect to the sql server. It gives the error: Couldn’t connect to SQL Server on IPAddress/SQLExpress
I tried checking all configurations like TCP/IP through SQL Server Config management.
Can someone please help?
Ensure remote connections over named pipes are enabled and make sure you're using a backslash before the instance name:
$myServer = "IPAddress\SQLExpress ";
If you'd like to enable connection over the default port (1433) this answer will help.

Database code is not connecting in chat program using php

<?php
// MySQL database connection file
$SERVER = "127.0.0.1"; // MySQL SERVER
$USER = "root"; // MySQL USER
$PASSWORD = "admin"; // MySQL PASSWORD
$link = #mysql_connect($SERVER,$USER,$PASSWORD);
$db = mysql_select_db("website");
?>
This is a database connecting code for chat program but it not connecting,Can any one pls help me to correct this code?
Drop the # infront of mysql_connect, it's used to suppress error which you don't want.
Also you need to check the return value of mysql_connect which is there in $link and make sure that it is not false before you proceed and to a DB select. Calling the function mysql_error when an error occurs gives you the reason for the error.
$link = mysql_connect($SERVER,$USER,$PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

Categories