oci_connect fails from command line but works in browser - php

I wrote a PHP script to run as a cron job and it wasn't running. The script worked fine in the browser, but I discovered when I ran it from the command line that I got this error:
Warning: oci_connect(): ORA-12154: TNS:could not resolve the connect identifier specified in /usr/apps/webdata/cron/PropogateDB.php on line 190
The line in question is:
$conn_NRB = oci_connect($user, $pass, "nrb.njbbnrbpd1");
As I said this script works fine in the browser.
I did some digging around and found the configuration settings for the database. I then tried this:
$conn_NRB = oci_connect($user, $pass, "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=nrb)))");
With this code both the command line and the browser give me this error:
Warning: oci_connect() [function.oci-connect]: ORA-01017: invalid username/password; logon denied in /usr/apps/webdata/cron/PropogateDB.php on line 191
As far as I know there are no other usernames and passwords that I can use. (I'm trying to verify that now.)
I searched around on this site and discovered someone else with a similar problem, although his problem was reversed, and his cause was that the command line and the browser pointed to two different php.ini files. I just confirmed that both the browser and the command line invoke the same php.ini file. They both point to:
Configuration File (php.ini) Path => /usr/local/php/lib
Is there any other reason why a script would work in the browser but fail in the command line? I'm relatively new to Oracle SQL and I don't have direct access to the database so I'm not sure where to go from here.
Thanks.

The 'EasyConnect' string
(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=nrb)))
refers to localhost. 'In the browser' likely means: on the server (where the PHP script executes).
Unless you run the test on the command - line of the same server, you should change the connect string to refer to the Database server (which might be simply the host name of your web-server).
Beware of firewall restrictions that may come in the way. Prefer to use your company's tnsnames.ora to resolve database instances identifiers to their connect strings, if possible.

Related

phpsdk_deps returns fatal error on windows

To compile PHP 7.1.X on Windows I use steps written here:
https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2
When I try to update dependencies with following command:
phpsdk_deps -u
I get following error:
D:\php-sdk2\php-sdk-binary-tools\phpdev\vc14\x64_71_2\php-7.1.15-src
$ phpsdk_deps -u
Fatal error: Uncaught SDK\Exception: Failed to fetch supported
branches
This is happening for almost two weeks now.
You're having network trouble.
If you open up the Config.php file at this line, you'll see that the Config tries to load the URI https://windows.php.net/downloads/php-sdk/deps/series/, and pick the correct txt file from there, in order to download the right dependencies. The exception you get means that this cannot be properly fetched.
The solution in my case was to use the company proxy (command line wasn't using it, so it cannot properly resolve & reach the above URL, and so the Config.php gets an empty response). How to do so might change depending on the network settings you use. In my case, I simply visited the URL above in Firefox (which is properly configured by the company), opened up the network pannel, get the IP of the website from there (which is actually the IP of the proxy Firefox was told to use by the company settings), and add the IP hostname entry in my host file (C:\Windows\System32\Drivers\etc\host) so it looks like 10.1.2.3 windows.php.net
Then I rerun the Config script, and boom, it managed to retrieve the txt files and the dependencies.
Another solution can be to retrieve all the dependencies listed in the txt file that corresponds to your setup, but it's a bit tedious...

ISAM Table Not Found, SQL state S0000 in SQLExecDirect

PHP Version: 7.0.14
Working on Windows Server 2012
Database files are in D: drive
Website/Application is assigned to the Application pool correctly
Assigned Application pool has the permissions to D: drive
Trying to connect and fetch information from ToppSpeed Database.
Created DSN correctly using Topspeed ODBC Driver.
I have created a sample script to verify if it is working or not.
odbctest.php
$conn=odbc_connect("DSN_NAME",'',''); //Make a coonection to DSN
if (!$conn){
exit("Connection Failed: " . $conn);
}
$sql="SELECT * FROM ExampleTableName"; // Query String
$rs=odbc_exec($conn,$sql); //Execute Query <--- Error Line
if (!$rs) {
exit("Error in SQL");
}
while (odbc_fetch_row($rs)){
var_dump(odbc_result($rs,1));
}
odbc_close($conn);
Error:
PHP Warning: odbc_exec(): SQL error: [SoftVelocity Inc.][TopSpeed ODBC Driver][ISAM]ISAM Table Not Found, SQL state S0000 in SQLExecDirect in C:\inetpub\wwwroot\projectFolder\odbctest.php on line 9
Working: When I run same file on PHP inbuilt server using command > php -S localhost:8002
and run file in a browser at http://localhost:8002/odbctest.php Everything works fine.
Working: When I run file in command line > php odbctest.php It works fine I got data from the table.
Not Working: I have added the website in IIS Manager, bind the website(physical path is C:\inetpub\wwwroot\projectFolder ) with the local IP
When I open that odbctest.php in a browser with local IP I am getting Table NOT Found error.
Same Script, Only difference is it is running on Command Line and not on IIS server, It is running on PHP inbuilt server and not working on IIS.
other Technical Difference is
I have put one PHP file to see PHP information on both servers.
Here is the only difference I found in that.
Working On PHP inbuilt server: In PHP info Server API is: Built-in HTTP server
Not working on IIS Server: in PHP info Server API is: CGI/FastCGI
there is not issue in DSN string because when I change the DSN string with unknown it identifies that there is something wrong in DSN string.
I think there might be some permission issue when trying to access data from IIS server (PHP runs on CGI/FastCGI).
I am wondering if there is permission issue why the error says table not found!!
I don't find any good documentation for TopSpeed Database connection error as this database is not widely used.
Any suggestion or idea? Please advise. Thank you in advance.
The database SQL error was not stating anything about permission,
so I created a simple another PHP script to append data to TEXT file.
I ran that PHP script with two test cases on the IIS server
Test Case 1: Write in a text file which resides in the project directory(same server)
And PHP script executes correctly and it wrote data to text file.
Test Case 2: I change the text file path with the file D:\some_directory\logfile.txt (Resides on D drive)
And when I ran it again script again I got the following error:
Warning: file_put_contents(D:\some_directory\logfile.txt):
failed to open stream: Permission denied in
C:\inetpub\wwwroot\project-directory\write.php on line 12
It confirms there is some problem with the Permissions
App pool assigned to the Website in IIS manager already had the permission to D: Driver. So it was not the problem.
In more research, I found that
we need to identify the USER first that we need to provide the permissions and the user is the value of "Anonymous user identity"
https://stackoverflow.com/a/32033941/5236174
https://www.iis.net/configreference/system.webserver/security/authentication/anonymousauthentication?showTreeNavigation=true
I saw that the value of Anonymous user identity was not the Application Pool!!
I changed that to the Application pool identity.
So now the website/application's anonymous user identity become the App pool and that has correct permissions.
Afthesethis changes, everything works perfectly fine as there is no permission issue.

PHPUnit database connection error

Very simple-to-explain problem here (at least after struggling with it and simplifying it almost up to the absurd).
I do a connection via the OCI driver inside a file called whatever.Test.php. Just that, make a simple query, and exit. I then call the file from the browser, and the data taken from the database is properly displayed. Next step: I run PHPUnit over the same file, with the following result:
PHP Warning: oci_connect(): ORA-12154: TNS:could not resolve the connect identifier specified in ...
Any ideas?
The final solution was a little bit obvious once I came up with it. Somehow the server could figure out what the server I was trying to connect with was, via the tnsnames.ora file somewhere, I guess. However, since the script execution doesn't trigger all the server processes this information was not reachable any more from there. So what I needed to do is to provide all the data in the oci_connect() $connection_string parameter, using the format [//]host_name[:port][/service_name][:server_type][/instance_name] (check documentation). Previously I was only giving the host_name part.
(Thanks anyway for your reply, user*).

Connecting to MYSQLi through Terminal in Mac OS using MAMP

I am trying to run my PHP script via Terminal in Mac. When I try to run the script, I am getting this error:
Warning: mysqli_connect(): (HY000/2002): No such file or directory
This is how I am trying to connect:
mysqli_connect('localhost','root','root','my_db');
I also tried to connect using the host: 127.0.0.1 instead of localhost and I then got this error:
Warning: mysqli_connect(): (HY000/2002): Connection refused
From what I've read online it might have something to do with mysql socket, but whatever I try to do to get this to work doesn't help.
I even tried to connect using the following as my host:
:/Applications/MAMP/tmp/mysql/mysql.sock
:/var/mysql/mysql.sock
Any idea what can be the problem/solution?
Had the same issue. Here's what I did.
Open Terminal (/Applications/Utilities/terminal.app)
Use your favorite text editor to create/edit ~/.bash_profile. For instance, if you're using vim, type vim ~/.bash_profile. I use TextMate, so I typed mate ~/.bash_profile.
Add these lines:
export MAMP_SQL=/Applications/MAMP/Library/bin
export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.10/bin
export PATH="$MAMP_SQL:$MAMP_PHP:$PATH"
These lines ensure that the first versions of MySQL and PHP that are found (and therefore used) are the versions used by MAMP. NB: Make sure that php5.5.10 matches the version MAMP is using. You can check this against http://localhost:8888/MAMP/index.php?page=phpinfo&language=English by default. SAVE THE FILE (I shouldn't have to save this, but invariably someone complains that it doesn't work).
Close your terminal window and reopen. This restarts the shell, which will load your .bash_profile script.
Type which php. You should get something akin to /Applications/MAMP/bin/php/php5.5.10/bin/php
Type which mysql. You should get something akin to /Applications/MAMP/Library/bin/mysql
If for any reason you get something different than the responses from 5 and 6, go back and check your .bash_profile; chances are you just neglected to save it properly.
If you have MySQL installed on your computer, you can do the following command:
mysql -u "username"
replacing "username" with the username of your computer
NOTE: often times root does not work, try the username of your computer

PHPUnit and mysqli_connect generate warning

I'm new to unit testing and want to start using for a large project I'm working on. I've got PHPUnit running on my Mac (10.8.3) alongside MAMP. The application I'm writing unit tests for relies heavily on the database.
In one of the classes I'm testing, it creates a connection to a MySQL database using mysqli_connect - however it is failing with this error:
Warning: mysqli_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /[PATH TO FILE]/database.php on line 340
I am I unable to connect to a database when running unit tests, or is there something extra I need to setup? The database is also running locally on my machine.
Here is the solution of your problem(Hope so).
Check the following link:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
mysql_connect-no-such-file.
This error occurs due to the setting in php.ini file. you need to alter that file.
Hope this will provide you some solution.
Turned out to be an issue with PHPUnit look for a MySQL socket in the wrong location - the one set in php.ini was correct.
Following the last command from this tutorial solved the issue: http://tommcfarlin.com/phpunit-wordpress-mamp/

Categories