What might be the reason of the following problem.
PHP function mysql_connect() works fine if I execute it from
command line (php -r "mysql_connect('127.0.0.1', 'db_user', 'db_pass');").
The same call with the same parameters fails for PHP running as an Apache module.
MySQL server is running remotely, connection to it is forwarded using SSH: ssh -fN -L 3306:localhost:3306 remote_host
Any ideas what might be wrong?
You'll want to be looking at using MySQLi or PDO as the older MySQL functions are deprecated.
This should help get you started:
<?php
//establish conection
$link = mysqli_connect("host","user","pass","bd") or die("Error: " . mysqli_error($link));
$query = "SELECT name FROM mytable" or die("Error: " . mysqli_error($link));
//execute query
$result = $link->query($query);
//display information
while($row = mysqli_fecth_array($result)) {
echo $row["name"] . "<br>";
}
?>
Based on your question and details, I would say that the MySQL server is refusing to connect to your web server (where ever it is) because the connection is not secure (in one fashion or another). If you want to connect via the PHP module in Apache as easily as you do from the command line, set up your connection so that all encryption/ tunneling issues are resolved. That's what I would do.
However, make sure the following line is correct..
mysql_connect('127.0.0.1', 'db_user', 'db_pass');"
Are you pointing to the correct server? I thought you said that the MySQL server was remote. This line suggests that you are attempting to connect to a local, imaginary MySQL server that is running on the same machine as the web server. Try getting the IP or domain name of the MySQL server and use that instead (in your web code, that is).
Related
I have neo4j running on my localhost port 7474 on MAC and I would like to access the same from server http://www.example.com/test/
How can I do that? I tried adding IP on remote mysql in cpanel on server and its not working. Is there any other setting I should do/add on MAC ?
php code:
<?php
// connect
$cs = mysql_connect ( '123.123.123.123:7474', '', '' ) or die ( 'Can not connect to server' );
?>
error:
PHP Warning: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Can't connect to MySQL server on '123.123.123.123' on line 4
also:
$link = mysqli_connect("123.123.123.123:7474","","","") or die("Error " . mysqli_error($link));
Error:
Unknown MySQL server host
mysqli_error() expects parameter 1 to be mysqli, boolean given
neo4j is not mysql, and cannot be accessed through a mysql driver.
Visit this URL for more information on accessing neo4j from PHP:
http://neo4j.com/developer/php/
You can test access using the neo4j-shell as well, if you have SSH access to your remote server:
http://neo4j.com/docs/stable/shell-starting.html
Also, if you want to access neo4j running on your local Mac from a server hosted in some datacenter somewhere, you'll need to make sure you can route the traffic to your local Mac. It seems like you've figured out how to do that (based on the MySQL errors).
For the reference of others, you'll need to adjust conf/neo4j-server.properties to reference 'org.neo4j.server.webserver.address=0.0.0.0'. This will make sure that neo4j binds to all IP addresses on the local box and not just 127.0.0.1. Additionally, if your Mac is behind NAT (network address translation), then your IP address will not be directly Internet-accessible (ie, if it's something like 192.168.1.12). In this case, you'll want to do port mapping on your router to map your real IP address:7474 to 192.168.1.12:7474, and then connect to your real IP address:7474 when accessing from an external server.
you cannot pass $link as argument for mysqli_error because it's not defined yet
$link = mysqli_connect(...) or die("Error " . mysqli_error($link));
proper way is e.g:
$link = mysqli_connect(...);
if ($link->connect_errno) {
die("Error " . mysqli_error($link));
}
I don't know if neo4j is running as MySQL server, than probably you need other database driver for PHP
I am new to PERL script and I want to collect data from PHP/MySQL in a Linux server, using PERL script.
I have tried some sample PERL code and it works on a local environment.
But I need to get data from another Linux server:
#!/usr/bin/perl
use DBI;
# connect to MySQL...
$driver= "mysql";
$dsn = "DBI:$driver:database=mms;host=localhost";
$dbh = DBI->connect($dsn, "root", "");
# prepare and execute the SQL statement
$sth = $dbh->prepare("SELECT * FROM dailyproductions");
$sth->execute;
# retrieve the results
while( my $ref = $sth->fetchrow_hashref() ) {
print $ref->{'id'};
}
exit;
This code works locally (localhost), but when I switch to a different host:
$dsn = "DBI:$driver:database=mms;host=192.168.0.1";
I get this error:
DBI connect('database=mms;host=192.168.0.1,'root',...) failed: Host 'admin-
lap' is not allowed to connect to this MySQL server at D:\Confidential\Report\mysql.pl line 7
Can't call method "prepare" on an undefined value at D:\Confidential\Report\mysql.pl line 10.
How can I overcome this?
You're not using "phpmysql", you're using MySQL. Try not to get the two confused.
Your program works. You've established that. The problem is with the MySQL connectivity between the machine where you're running the program and the machine with the MySQL database server. That's what your error message is saying.
Host 'admin-lap' is not allowed to connect to this MySQL server
You will need to get the MySQL configuration changed to allow your host to connect to the server. Your database administrator will be able to help with that.
One fix I would make to your program. You see the second error, about calling prepare on and undefined value? That's because your program assumes that it can always connect to the database. You should change that so that the program dies if it can't make the connection.
$dbh = DBI->connect($dsn, "root", "")
or die $DBI::errstr;
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
I'm trying to connect to remote DB2 via PHP. But have some problems. I've already installed IBM Application developer client.
phpinfo() output:
IBM DB2, Cloudscape and Apache Derby support enabled
Module release 1.9.4
Module revision $Revision: 327944 $
Binary data mode (ibm_db2.binmode) DB2_BINARY
Then, I've got a php file which is looking like:
$database = 'MyDB';
$user = 'db2inst1';
$password = 'mypassword';
$hostname = '1.1.1.1';
$port = 50000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;PORT=$port;HOSTNAME=$hostname;".
"PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "connection to $database succeeded";
} else {
echo "connection to $database failed";
echo db2_conn_errormsg();
}
And trying to execute this file, I have "connection to MyDB failed", and NO visible response from db2_conn_errormsg(), which is actually making me baffled
Unfortunately, I haven't got a straight access to the remote server with database. But several months ago, when I was using other client, I succeeded to connect to exactly this database. But that time I didn't need to install IBM ADCL. That is why I can guess that problem is on this side. But even if so, I couldn't fix it.
Sorry if I duplicated some question on stackoverflow, but all answers, which I found, were unfortunately useless to me.
I'm using Apache 2.2 and PHP 5.4.
Hope you can help.
Thanks for any replies!
Are you sure you have connectivity to the server? Correct port, server, firewall rules, username, password, database name?
What is the SQL code you are receiving. Try to get the SQL code from PHP, "connection to xx failed" is your own code so it is useless to help you.
Did you install the application development client? which DB2 version are you using? ADCL is old, it was for DB2 8. Since DB2 9.7, clients have different names, and I think you need IBM Data server client in order to compile the php module. For more information, check this website: http://www-01.ibm.com/support/docview.wss?uid=swg27016878
I think you have to catalog the database server (node) and the database in the local machine with the db2 client. It seems that your PHP code uses ODBC driver, and it has to be configured locally.
Your connection string looks like an ODBC connection where as the db2_connect function in PHP needs :-
DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;
It's on the PHP web page.
I have never been able to get ibm_db2 or pdo_ibm working on a remote. They work if DB2 and Apache are on the same machine (like the iSeries) but not if the host is connecting to a remote DB2.
If you read the Doctrine2 PHP drivers for each you will find they redirect to ODBC if the host is not 'localhost' or '127.0.0.1'.
This code works for me
<?php
if (!$db = odbc_connect ( "AS400", $user, $password) )
echo 'error!';
$result = odbc_exec($db, "select count(*) from $table");
while (odbc_fetch_row($result)) {
var_dump($result);
print_r($result);
echo "\n";
echo odbc_result($result, 1)."\n";
}
odbc_close($db);
AS400 is DSN name defined in ODBC.
My current situation is that I'm using:
1) Mysql Version 5.1
2) Apache Server 2.2
3) Php Version 5.2.17
And... I have the simplest PHP Code Here.
But poorly, the connection is always failed (On Browser). I tried to follow some steps from the StackOverFlow, and also some other forums such as here;
I already tried to:
1) Ensure the System32\drivers\etc\host file is using the safe
Host File Content Here.
2) Ensure the Apache is having Safe Error Content Here.
3) Ensure the mysqli & mysql extension of PHP is Installed
properly accordingly.
But then... The latest result is still same, problem still exist. Sigh. Is there something I forgotten?
if you are using Windows 7 then you need to change localhost to 127.0.0.1.
Is the database running? You need to check that it is started by going to the MySQL bin directory and running mysql command (if you are requested for a password, just hit enter). If you connect then the user is root and the password is blank
What username and password are you using to access the database?
Why are you using #mysql_connect? remove the "#" symbol
<?php
$url = 'localhost';
$link = mysql_connect($url, 'user', 'pass');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>