PHPUnit and mysqli_connect generate warning - php

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/

Related

How to run a Laravel application that was not created by me on my personal server

I need to alter some of the front-end features of a Laravel application that was not created by me. The application was created using Laravel and am not sure how to get it up and running so I can alter the front-end code. I was given a copy of the source files. I then put that source files in to my xampp/htdocs folder. I started Xampp, opened the folder in Gitbash and entered PHP artisan serve. The application does not appear to be running.
Is there something I am missing or doing wrong? Please take into consideration that I did not create this application, for your information, I do know how to create and run a Laravel app locally, but am wondering if the process is different if it was not created by me personally?
Thank you all for helping me out, I appreciate you personally and this community.
Here is the error I am receiving in Gitbash:
PHP Warning:
require(C:\xampp\htdocs\appoets\bootstrap/../vendor/autoload.php):
failed to open stream: No such file or directory in C:\xampp\htdocs\appoets\bootstrap\autoload.php on line 17
Warning: require(C:\xampp\htdocs\appoets\bootstrap/../vendor/autoload.php):
failed to open stream: No such file or directory in
C:\xampp\htdocs\appoets\bootstrap\autoload.php on line 17
PHP Fatal error: require(): Failed opening required
'C:\xampp\htdocs\appoets\bootstrap/../vendor/autoload.php'
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\appoets\bootstrap\autoload.php on line 17
Fatal error: require(): Failed opening required 'C:\xampp\htdocs\appoets\bootstrap/../vendor/autoload.php'
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\appoets\bootstrap\autoload.php on line 17
Please solved this for me. If you run into the same error. Have the correct file path open in Gitbash and then run "composer install"
After that was fixed another problem popped up. I went to load Laravel in my browser and it gave a general error message no error number or anything. To fix this go to your env.example file and rename it. .env then go to the terminal or your command line and run php artisan key:generate. After this hit save all and go refresh. All should be fixed!
The errors your getting are because you haven't installed your composer dependencies. Install them with composer before running the application:
composer install
Here's the old answer before that callstack was provided. I think it's still interesting background information for you, but not immediately relevant.
It looks like you're running two web servers so I suspect you're just
confused about where the application is running.
XAMPP ships with Apache. I'd expect that running it will spin up its
web server on port 80, the default port that web browsers use.
Therefore, if you're trying to get to the application at
http://localhost, you're hitting this webserver, which might have a
different (and not set up) application on it.
Running php artisan serve will start a second web server utilizing
PHP's built in web server. According to the documentation, the
default port that this command will use is 8000:
By default the HTTP-server will listen to port 8000. However if that
port is already in use or you wish to serve multiple applications this
way, you might want to specify what port to use. Just add the --port
argument:
Therefore, you can access the application without XAMPP though this
url: http://localhost:8000
Try both URLs.
Note that php artisan serve doesn't start up a MySQL server. You
will still need XAMPP to start the MySQL server if you're using a
database.
Checklist for your issue
1. composer update --no-scripts or composer install
2. php artisan key:generate
3. rename example.env to .env
4. Change database credentials, set debug mode! Change app url
4. php artisan migrate(make sure u have the database running in the server)
5. php artisan config:clear
6. you are good to go!

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.

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

oci_connect fails from command line but works in browser

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.

create mysql database on mac os x

I know this sounds a little ridiculous, but I'm just honestly wondering how I can create a MySQL database on my Mac OS X machine. I have MySQL installed (as far as I know) as well as PHP, and I'm wondering how I can create one.
I have downloaded a sample PHP ecommerce site to kind of delve into what I'm trying to familiarze myself with, and the PHP files are attempting to connect to a nonexistant database, which I'm hoping to create right now.
Do they have an extension of .sock? Because, here is the "error" code when I try to test a PHP page referencing the nonexistant database:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in< /Library/WebServer/Documents/plaincart/library/database.php on line 4 Warning: mysql_connect(): No such file or directory in
/Library/WebServer/Documents/plaincart/library/database.php on line 4 MySQL connect failed. No such file or directory
My guess is you are specifying the database server as localhost somewhere. Try 127.0.0.1 instead.
I had this problem once with WordPres on Mac OS X. The answer was to name the local machine in the db connection configuration 'Localhost' with capital 'L', because on OS X localhost != Localhost.
I had the same problem with MySQL on the OSX. It turned out that the MySQL server was actually set up to use a different socket than the default setting in PHP.
PHP was looking in /var/mysql/mysql.sock, while mysql was creating the socket in /tmp/mysql.sock.
I fixed it by editing the php.ini and set mysql.default_socket = /tmp/mysql.sock

Categories