MySQL remote connection with PHP - php

I'm new to MySQL and PHP and using 5.0 version of php. My requirement is to connect to a MySQL database which is located on another server (www.some-domain.com). My PHP files are located in my local system.
I have remote server database credentials. How do I configure these details into the PHP file ?
Here is my code so far:
mysql_connect("remote server ip", "root", "root123") or die (mysql_error ());
// Select database
mysql_select_db("resource") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM resource WHERE resource_type=11";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
when i use above code getting below error:
Access denied for user 'root'#'%' to database 'resource'

To connect to your mysql database in another server, you need to get the host, username and password of the database on that particular server. Like,
mysql_connect('server_host', 'server_db_username', 'server_db_password');
mysql_select_db('server_db_name');
Your code is correct. You only need to connect with the server credentials.

There is a possibility, that remote connections are not allowed within MySQL Server access rules, as a common security measure. I suggest you to contact server support team for further details.
Also, I suggest to read this.

Related

mysql_connect(): Connection refused

I was just wondering if it was possible for certain databases to block SQL accesses through mysql_connect. I recently downloaded XAmpp and created a SQL database using my own computer as a local host and wrote a PHP file that accessed that database.
However, when I tried to change the username and host settings so that Xampp would run the PHP files that would then connect to the external SQL database of a free hosting site that I'm trying to use (biz.co.nf), I got the following error message:
Warning: mysql_connect(): Connection refused in /Applications/XAMPP/xamppfiles/htdocs/...
Right now I'm thinking either:
My login credentials to that database are incorrect, or
Somehow the host blocks SQL accesses from external users, but if I were to load my php code into the server using FileZilla, it should work okay?
Please let me know if this is the case.
I'm also fairly certain I have the right login credentials.
Here's what I have:
$host = "fdb13.biz.nf";
$username = "1764941_login";
$password = ________;
$db_name = "1764941_Login";!
$tbl_name = "Members";
//Connect to server
mysql_connect($host, $username, $password)or die("Cannot Connect!");
mysql_select_db($db_name)or die("Cannot select Database");
with my server settings according to my website...
Of course, I omitted my password.
Connections are refused from remote locations to MySQL for security reasons. You can add your IP Address to enable MySQL to listen from your database by following the instructions in this link - Cyberciti biz tips on enabling remote access to mysql database server In your case, add the IP address of the location where your PHP script.
I would also say that
mysql_connect
is deprecated in PHP. Use mysqli instead. Check the comments in the link - PHP original MySQL API

Setting up a local call to DB with an online PHP file - trouble connecting

Trying to figure out how to connect to a local SQL Server with an online PHP file. However I am getting connection errors.
Warning: mysql_connect() [function.mysql-connect]: [2002] No
connection could be made because the target machine actively refused
it. (trying to connect via tcp://localhost:xxxx) in
E:\web\example.com\uploads\readDB.php on line 8
In particular I am having trouble trying to specify if, and where, I do the connection to the PC and then to the database with any associated passwords.
My current connection looks like this
mysql_connect("localhost","the_sql_server_name\SQLEXPRESS","mysql");
mysql_select_db("ACME") or die(mysql_error());
$data = mysql_query("SELECT * FROM TradingAccount")
or die(mysql_error());
My server is run locally (sql server studio management 2012), and I am trying to connect to its specific DB called ACME, from the table TradingAccount.
Can someone show me how to connect to this local table successfully with PHP? I use Windows Authentication at the moment when I run the server as well, in case that matters.
I haven't been able to find any useful resources, and I have only ever used PHP to connect to online DB's as well, nothing locally before.
Your Problem is in this line:
mysql_connect("localhost","the_sql_server_name\SQLEXPRESS","mysql");
You wrote the_sql_server_name\SQLEXPRESS there. I don't understand why. The syntax of the method is:
mysql_connect("hostname", "username", "password");
For more information, visit the PHP manual entry for mysql_connect.

PHP: connect with external mysql database

I want to connect with external database using PHP. But, it gives this error
Warning: mysql_connect(): Can't connect to MySQL server on 'SERVER_IP_ADDRESS' (110) in /home/<username>/public_html/index.php on line 7
Can't connect to MySQL server on 'SERVER_IP_ADDRESS' (110)
Here is my code.
define("_CONFIG_DB_HOST", "SERVER_IP_ADDRESS"); /* database host name */
define("_CONFIG_DB_NAME", "DATABASE_NAME"); /* database name */
define("_CONFIG_DB_USER", "DATABASE_USERNAME"); /* database username */
define("_CONFIG_DB_PASS", "DATABASE_PASSWORD"); /* database password */
$con = mysql_connect(_CONFIG_DB_HOST,_CONFIG_DB_USER,_CONFIG_DB_PASS) or die(mysql_error());
mysql_select_db(_CONFIG_DB_NAME,$con) or die(mysql_error());
I've also added entry for Remote Database Access Hosts in Host Server
Please guide me if I'm doing anything wrong.
Make sure DATABASE_USERNAME has access from you host (the machine running this script) has access to DATABASE_NAME on SERVER_IP_ADDRESS.
When you grant access on database to use you would use USERNAME#HOST form. Here the HOST means the host where this USERNAME would be connecting from. Usually we keep it localhost. becasue we want to connect by a user who is in localhost according to mysql server. But your local host is not mysql servers localhost. That's why that host part needs to be changed.

Call to undefined function odbc_connect() message while connecting SAP Hana database

I used odbc_connect() in my PHP page to connect to the HANA database. It works fine when i run it locally.
I upload the same PHP page into the server and i am getting this error:
Fatal error: Call to undefined function odbc_connect()
The code:
$connect = odbc_connect("Team6DataSource", "TEAM6", "Password1", SQL_CUR_USE_ODBC);
Team6DataSource = datasource name.
ip address = 54.217.234.218
Can any one please help me?
Thanks
I just go through in google get this instruction this is really helpful for you.
Download the SQL Server ODBC driver for your PHP client
platform. (Registration required.) If the SQL Server ODBC driver
is not currently available for your platform, check the list of
ODBC-ODBC Bridge Client platforms. The ODBC-ODBC Bridge is an
alternative SQL Server solution from Easysoft, which you can
download from this site.
Install and license the SQL Server ODBC driver on the machine where
PHP is installed. For installation instructions, see the ODBC driver
documentation. Refer to the documentation to see which environment
variables you need to set (LD_LIBRARY_PATH, LIBPATH, LD_RUN_PATH,
SHLIB_PATH depending on the driver, platform and linker).
Create an ODBC data source in /etc/odbc.ini that connects to the
SQL Server database you want to access from PHP. For example, this
SQL Server ODBC data source connects to a SQL Server Express instance
that serves the Northwind database:
Use isql to test the new data source. For example:
cd /usr/local/easysoft/unixODBC/bin
./isql -v MSSQL-PHP
[MSSQL-PHP]
Driver = Easysoft ODBC-SQL Server
Server = my_machine\SQLEXPRESS
User = my_domain\my_user
Password = my_password
Please copy and paste this script and execute this
<?
/*
PHP MSSQL Example
Replace data_source_name with the name of your data source.
Replace database_username and database_password
with the SQL Server database username and password.
*/
$data_source='data_source_name';
$user='database_username';
$password='database_password';
// Connect to the data source and get a handle for that connection.
$conn=odbc_connect($data_source,$user,$password);
if (!$conn){
if (phpversion() < '4.0'){
exit("Connection Failed: . $php_errormsg" );
}
else{
exit("Connection Failed:" . odbc_errormsg() );
}
}
// This query generates a result set with one record in it.
$sql="SELECT 1 AS test_col";
# Execute the statement.
$rs=odbc_exec($conn,$sql);
// Fetch and display the result set value.
if (!$rs){
exit("Error in SQL");
}
while (odbc_fetch_row($rs)){
$col1=odbc_result($rs, "test_col");
echo "$col1\n";
}
// Disconnect the database from the database handle.
odbc_close($conn);
?>
Replace data_source_name, database_username and database_password
with your SQL Server ODBC data source, login name and password.
To run the script under Apache, save the file below your Apache web
server’s document root directory. For example,
/var/www/apache2-default/php-mssql-connection.phtml. Then view the
file in a web browser:
http://localhost/php-mssql-connection.phtml
If your web browser is not running on the same machine as the web
server, replace localhost with the web server’s host name or IP
address.
To run the script from the command line, save the file.
For example,
/tmp/php-mssql-connection.php. Then run $ php
/tmp/php-mssql-connection.php.
further more Details Refer this LINK
Download this, copy the .dll to PHP folder and in the php.ini file add:
extension=php_sqlsrv_7_ts_x64.dll
extension=php_pdo_sqlsrv_7_nts_x64.dll

Narrow down php-sql connection failure to server settings?

I am trying to connect to a MYSQL database on server A from server B. The hosting company that we are working with, owns both server A and server B. This is my first attempt at an external DB connection.
I have written the following PHP code to try to connect to the MYSQL database on server A from server B. The code looks like most other code I have Googled in regards to connecting to external MYSQL databases..
$IPAddress_O_fServer_A = 'XXX.XX.XX.XXX';
$Server_A_DB_Pass = 'P-WORD';
$Server_A_DB_User = 'U-NAME';
$con = mysql_connect($IPAddress_Of_Server_A, $Server_A_DB_User, $Server_A_DB_Pass);
Now when this code executes on Server B, I get this error:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'XXX.XX.XX.XXX' (4) in /var/www/web12/web/dev/front-end.php on line 10
Could not connect: Can't connect to MySQL server on XXX.XX.XX.XXX' (4)
...where line 10 is the mysql_connect() call.
Does anyone see anything wrong with this code?
If the connection is not working would it be safe to say that there must be an issue with either the servers external connection permissions or some other settings?
Thanks!
First off I'd look at this and make sure that you're system is configured to allow remote access.
Secondly the user name "U-NAME" will actually be "U-NAME#localhost" and will have permissions set up as such. For example consider the following GRANT statement.
GRANT SELECT *
ON foo.bar
TO 'U-NAME'#'localhost'
The user name "U-NAME" has permission to select entries from the database foo, table bar, but it is exclusively localhost access. In order to allow remote you'd have to grant permissions similarly to the following.
GRANT SELECT *
ON foo.bar
TO 'U-NAME'#'xx.xx.xx.xx'

Categories