I have a simple php file I want to execute in the terminal:
script.php
echo "script is running";
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Connection Established\n";
}
mysql_close($con);
I'm running the script in the terminal on MacOs with php script.php, and I'm getting the following error:
script is running
Warning: mysql_connect(): No such file or directory in
/Applications/MAMP/htdocs/issueManager3/notifications.php on line 5
Could not connect: No such file or directory
However, when I run the script through a web browser, it connects to the database fine.
Am I missing something? Or is there something else I need to set up to achieve database connections through the terminal?
Any help appreciated.
Related
I run my website on Debian 9, Apache 2.4, PHP 7.2. MySQL database is remote. The website works smoothly using mysqli module. But when I try to run PHP file through command line it fails and displays the error:
PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory in ...
Database connection failed: No such file or directory (2002)
The PHP file I've been trying to run has this line in the beginning:
require_once(__DIR__ . '/../private/initialize.php');
The initialize.php file has this line: $database = db_connect();, which calls these functions:
function db_connect() {
$connection = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
confirm_db_connect($connection);
return $connection;
}
function confirm_db_connect($connection) {
if ($connection->connect_errno) {
$msg = 'Database connection failed: ';
$msg .= $connection->connect_error;
$msg .= ' (' . $connection->connect_errno . ')';
exit($msg);
}
}
For security reasons database credentials is set as constants and is taken from environmental variables by the following code:
define('DB_SERVER', getenv('DB_SERVER'));
define('DB_USER', getenv('DB_USER'));
define('DB_PASS', getenv('DB_PASS'));
define('DB_NAME', getenv('DB_NAME'));
Environmental variables defined in /etc/apache2/envvars file through export command:
export DB_SERVER='*here db credential*'
export DB_USER='*here db credential*'
export DB_PASS='*here db credential*'
export DB_NAME='*here db credential*'
I have checked whether the environmental variables are being taken by the script and they weren't.
echo getenv('DB_SERVER');
echo getenv('DB_USER');
echo getenv('DB_PASS');
echo getenv('DB_NAME');
It seems that the variables are set improperly.
Can anybody help with the issue? I want to run PHP files with mysqli extension from command line so that to use CRON Jobs.
Thanks in advance!
I solved the issue!
I placed my variables into crontab -e file before cron jobs but without export command.
DB_SERVER='*here db credential*'
DB_USER='*here db credential*'
DB_PASS='*here db credential*'
DB_NAME='*here db credential*'
It makes the variables accessible for all cron jobs.
So when I am using cron jobs it works just fine!
I am trying to connect to an oracle database using php. When I try to connect by connection string/descriptor, it connects just fine, but when I try to connect using a tns name, the whole thing stops and chrome shows a blank error page ERR_CONNECTION_RESER. I see the failed response and it is 0 bytes, no header, no body... nothing.
here is the code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$connStr = "Some connection <string> copied from tnsnames.ora";
//$connStr = "Some connection <name> copied from tnsnames.ora";
try
{
$conn = oci_connect('dev', '12345678', $connStr, 'utf8');
if (!$conn)
{
$err = oci_error();
var_dump($err);
}
oci_close($conn);
echo "Success";
}
catch(Exception $e)
{
$err = oci_error();
var_dump($err);
var_dump($e);
}
I checked and my php can see
ORACLE_HOME
TNS_ADMIN
and they are pointing to the right direction.
I am using
Windows 7 amd64
PHP 5.6.13 TS VC11
Apache 2.4.16 VC14
Instant Client 12.1.0.2
After hours spent and sleeps not slept on this issue, I found the solution.
I have an sqlnet.ora file that specifies timeouts and other network properties. Our databases recently joined the firm's domain and the problem was with this line:
Previously
NAMES.DIRECTORY_PATH= (HOSTNAME, TNSNAMES, ONAMES, EZCONNECT, LDAP)
#########
Solution
NAMES.DIRECTORY_PATH= (HOSTNAME, TNSNAMES, ONAMES, EZCONNECT, LDAP)
NAMES.DEFAULT_DOMAIN = FIRM.DOMAIN
I have this problem, i have created a php file which inserts data into a mysqldatabase on Mamp.
I can use the command mysql anywhere and it starts the mysql client.
But when i start my php script i get this error:
Warning: Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running? in
For testing i have used the default username and password "root". Anyone know what causes this?
I have also tried this command:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
For me it is working fine. Which hostname you are using in your php code?
sgeorge-mn:~ sgeorge$ cat con_mysql.php
<?php
$con = mysql_connect("127.0.0.1:3306","root","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Connection Established\n";
}
mysql_close($con);
?>
sgeorge-mn:~ sgeorge$ php con_mysql.php
Connection Established
I get a "Connection Refused" error when I run the following batch file:
#!/usr/bin/php
<?php
mysql_connect('127.0.0.1', 'root', '*******');
?>
where * is a working password.
I've tried localhost too, but I get the same problem.
I know the password is good because I am able to connect just fine using:
> /Applications/MAMP/Library/bin/mysql -u root -p
I can also connect when I run the script from a browser (but I have to change 127.0.0.1 to localhost).
I'm using MAMP v1.8 on a Mac OS Lion.
Try using this line instead
$sqlcon = mysql_connect("localhost", "root", "password") OR die("Could not connect to database: " . mysql_error());
It should show some more info about the error and if not check to make sure that in phpmyadmin(or SQLbuddy) the user has access to databases(it should because the default 'root' should. Also be sure that MAMP is set to online mode.
Also it appears that you missed a ";" at the end of line 2(the line with the connect). Also, you have )) at the end where it should be ); Add that to the end of the line and it should work.
mysql_connect('127.0.0.1', 'root', 'password') OR DIE(mysql_error());
I'm in the process of setting up PHP / MYSQL on a Windows Server 2003 machine, with IIS. I have installed PHP 5.3.8 and MySQL 5.5. PHP is configured to use FastCGI.
In php.ini I have the following lines uncommented:
extension=php_mysql.dll
extension=php_mysqli.dll
Here is my code:
<?php
$con = mysql_connect("127.0.0.1","root","test");
if (!$con)
{
die('Could not connect: $php_errormsg' . mysql_errno() . ": " . mysql_error(). "\n");
}
echo "connected";
?>
Here is the output:
PHP Warning: mysql_connect(): in C:\Inetpub\wwwroot\mysqltest.php on line 2
I can connect to the MYSQL database on this server from a PHP page running on another webserver - so I know that my MYSQL database is correctly accepting connections.
Any help on this would be greatly appreciated
To view your connection error, you can print the output of mysql_error.
$con = mysql_connect("localhost","root","test") or die('cannot connect : '. mysql_error());
Apparently I had a permissions issue. I was able to resolve it by following these steps:
From Windows Explorer, I...
right-clicked on C:\Inetpub
went to Properties > Security > Permissions
selected "Replace permission entries on all child objects with entries shown here that apply to child objects"
clicked Apply
restarted IIS by running iisreset from a command prompt.