in mac i want to create cronjob but when i run the php file with database i am getting error in terminal.
Warning: mysqli_connect(): (HY000/1049): Unknown database
And In the browser run perfect.
check your port that mysql run .
in many cases mysql run in port 3306 but in my mysql it run in port 3308
and I add this:
$conn = new mysqli("localhost", "root", "", "myDB","3308");
I had a similar problem. My code was like this:
$conn=mysqli_connect($servername,$username,$password,$dB) or die("connection failed");
echo "connection success";
Problem: I did not indicate the port number.
Solution: I just included the port number, like below:
$conn=mysqli_connect($servername,$username,$password,$db,"3308") or die("connection failed");
echo "connection success";
Is your PHP environment the same as your browsers PHP? command line php.ini can differ from, for example, your XAMP or other installed webserver
I would recommend checking this first
for me this did the trick
edit your .bash_profile file like
export PATH=/Applications/MAMP/bin/php/php7.1.1/bin:$PATH
edit the path to your PHP bin from your XAMP
If you have this code:
$conn = new mysqli("localhost", "root", "", "myDB");
Try with creating new database called myDB in localhost/phpmyadmin.
In my case I got similar error
Warning: mysqli_connect(): (HY000/1049): Unknown database 'login.db' in D:\xampp\htdocs\login\functions\db.php on line 2
so i change [$con=mysqli_connect('localhost', 'root', '', 'login.db');] into
[$con=mysqli_connect('localhost', 'root', '', 'login');]
Ii changed 'login.db' to 'login' and error disappear
For me, the problem was the "Port number" (on Step 3 of OpenCart 3.0.3.2 installation) which was set as 3306 but in MySQL, it was showing as 3308 so I just changed the same and all went good.
enter image description here
Related
Cannot figure out why I am getting this error?
Warning: mysqli_connect(): (HY000/2002): Connection refused in
Here the code:
$con = mysqli_connect('localhost:3306', '********', '********');
mysqli_select_db ($con, "id9873966_search") or die mysqli_error($con));
If you're using the standard port 3306 you should just be able to write
$con = mysqli_connect("localhost", "username", "password", "id9873966_search");
Assuming you actually have a db named id9873966_search
Additionally, if you're on Windows PC you can open CMD as admin and run
netstat -a -b
This will generate a list of services listening on what ports so you can verify you are in fact using port 3306 on localhost. You can also open "Services" in Windows and ensure that your DB is installed and listening.
It may also be necessary to add a firewall rule on you machine.
I'm trying to run a PHP Script that contains a MySQL connection, when I run the command I get the following error in the terminal:
'Warning: mysqli::__construct(): (HY000/2002): Connection refused'
Even with PDO, I get the error:
'Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in ...'
I'm running the script on Mac OS X, and I have MAMP as APACHE/MySQL local server.
The command that I use for running the script is:
'php main.php'
and the content of the 'main.php' file with PDO is:
$db = new PDO('mysql:host=localhost;dbname=test',root,root);
with mysqli connector:
$db = new mysqli("localhost", "test", "root", "root");
I tried with '127.0.0.1' instead of 'localhost' and I get the same errors in both cases.
First of all, using localhost as a domain name is not supported in PHP. It will not be resolved as 127.0.0.1.
Second, even using 127.0.0.1 might not work in servers with multiple interfaces or with special routing rules. Run mysqlcheck --help and take the IP address shown at the bottom in the "host" field. For example:
port 3306
host 192.168.1.23
Use the IP and port that MySQL recognizes:
$db = new mysqli("192.168.1.23:3306", "test", "root", "root");
I'm facing this problem since yesterday evening. I have been now over 6 hours on Google, trying to figure out what the solution could be.
I have tried this, setting the timeout on all php.ini files to 300 or unlimited, I have tried setting the buffer of MySQL higher, tried to restart both Apache and MySQL server, ...
Problem: What I'm trying to do, where the error occurs, is to execute a SOAP-command (command is correct) $this -> soap -> executeCommand(new SoapParam($command, 'command'));, which results in:
"Could not connect to host" --> if I set define('DB_HOST', '127.0.0.1'); or
Problem 2:
//Warning: mysqli::mysqli() [function.mysqli-mysqli]: (HY000/2003): Can't connect to MySQL server on 'blacktempel.dyndns-home.com' (10060) in C:\Users\NAME\Desktop\Server_09122014\Apache\_Server\htdocs\SOAPRegistration.php on line 109
//Warning: mysqli::close() [function.mysqli-close]: Couldn't fetch mysqli in C:\Users\NAME\Desktop\Server_09122014\Apache\_Server\htdocs\SOAPRegistration.php on line 154
$this -> db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); //Line 109
$this -> db -> close(); //Line 154
"Database connection failed: Can't connect to MySQL server on 'blacktempel.dyndns-home.com' (10060)" --> if I set define('DB_HOST', 'blacktempel.dyndns-home.com');.
My firewall is not the problem, I have checked this multiple times.
My hosts file contains this:
# 127.0.0.1 localhost
# ::1 localhost
MyExternalIp blacktempel.dyndns-home.com
SOAP:
define('SOAP_IP', '127.0.0.1');
define('SOAP_PORT', '7878');
MySQL is running on port 3306 and listening. My other defines, such as DB_USER, DB_PASS, are correct.
Q: Why won't it work ? What could be wrong ? Is there anything I could possibly have missed ?
Fixed the issue.
The SOAP problem was caused by a setting in the config file of the program accessed by SOAP.
I am really a beginner in PHP and Mysql. I made a database on static IP 192.168.1.211 which is based on CentOS(only Command prompt) and on this IP there is no other software like easyPHP, and I am working on static IP 192.168.1.20 based on Windows 7. I also installed easyPHP and Dreamweaver... using Dreamweaver I made one .php file and I tried to use the database which is on 192.168.1.20..
using below code
<?php
$server2 = '192.168.1.211';
$con = mysqli_connect(server2,'root','password','vvani');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
but I get an error as shown below
Notice: Use of undefined constant server2 - assumed 'server2' in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Failed to connect to MySQL: php_network_getaddresses: getaddrinfo failed: No such host is known.
How can I access the database on 192.168.1.211 from 192.168.1.20 in PHP code?
I also tried hard to find a solution from Google, but I am not getting a perfect solution.
Just read errors and correct it..
<?php
$server2 = '192.168.1.211';
$con = mysqli_connect($server2,'root','password','vvani'); // here, $server2 is variable, not constant
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Change server2 to $server2. If that still doesn't work then it's possible the other server does not allow requests in such a way so you will have to update the MySQL settings on that server to allow external access to the port (or otherwise forward it).
Thanks Elon Than and Explosion Pills,
I changed server2 to $server2, but I get new warning
Warning: mysqli_connect(): (HY000/1130): Host '192.168.1.20' is not allowed to connect to this MySQL server in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
And I research on it and I try to give remotely access to database on another server.
Follow some few steps and solved problem.
# mysql -u root -p
mysql> GRANT ALL ON Database.* TO <un>#'localhost' IDENTIFIED BY 'password';
Thanks again.
$db= mysqli_connect('localhost', "root", "root");
if(!$db) die("Error connecting to MySQL database.");
mysqli_select_db("onlineform", $db);
As I try to connect to the database like shown above, I am getting the following error:
Warning: mysqli_connect(): [2002] No such file or directory (trying to connect
via unix:///var/mysql/mysql.sock) in - on line 3 Warning: mysqli_connect():
(HY000/2002): No such file or directory in - on line 3 Error connecting
to MySQL database.
It's my first time using mysqli, I thought it would be a big improvement over mysql. I checked with MAMP if it's enabled and it is.
Any suggestions?
Check if your socket is correct. If not, override the default socket. I guess you have to try something like this in MAMP:
$link = mysql_connect(
':/Applications/MAMP/tmp/mysql/mysql.sock',
'root',
'root'
);