Connection issue with mysqli - php

$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'
);

Related

MySQL Error mysqli::__construct(): (HY000/2002): Connection refused

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");

Warning: mysqli_connect(): (HY000/1049): Unknown database in mac terminal only

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

mysqli::__construct(): MySQL server has gone away

I can't connect to my server. Here are the mistakes it gives me:
Warning: mysqli::__construct(): MySQL server has gone away in
C:\xampp\htdocs\www\PHP-Project\logic\db_connection.php on line 3
Warning: mysqli::__construct(): Error while reading greeting packet.
PID=8632 in C:\xampp\htdocs\www\PHP-Project\logic\db_connection.php on
line 3
Warning: mysqli::__construct(): (HY000/2006): MySQL server has gone
away in C:\xampp\htdocs\www\PHP-Project\logic\db_connection.php on
line 3
Fatal error: Maximum execution time of 30 seconds exceeded in
C:\xampp\htdocs\www\PHP-Project\logic\db_connection.php on line 3
This is my connection file:
define("HOST", "localhost:81");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "bulitfactory_person_cv");
$db_connection = new mysqli(HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($db_connection->connect_error) {
die("Connection failed. ".$db_connection->connect_error);
}
Please, help me!
try it to increase max_allowed_packet and restart the server
Add log_warnings=2 within [mysqld]section to your .ini/.cfg file, shutdown restart mysql and you should have maximum connection failure information in your error.log.

Setting up MySQL issue under Eclipse PHP

I have a snippet of code under the PHP file in Eclipse PHP.
$username = mysql_real_escape_string( $username );
It generates an error Warning:
mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /web_development/php_work/new_test/test.php on line 37
Looks like the Eclipse PHP couldn't connect to the MySQL of XAMPP, so How do I fix that?
I added two lines
#mysql_connect('localhost','root', '') or die('Could not connect to : ' . mysql_error());
#mysql_select_db('test') or die( "-Unable to select database");
but Eclipse still throws me.
mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Can't connect to local MySQL server through socket '/tmp/mysql.sock'
thx.

php page can't load MySQL's database

I'm having some problems while trying to connect a .php with mysql.
here's the connection.php code
<?php
$db_host =$_POST['localhost'];
$db_user =$_POST['root'];
$db_password = "";
$db_name = "prtcl";
?>
and this is the page where I actually use the connection
<?
include("connection.php");
?>
...
<?php
$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $db);
if (!$db)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($db);
?>
<body>
...
that's what I get when I try to load it ( line 29 is this one:
$db = mysql_connect($db_host,
$db_user, $db_password);
)
Notice: Undefined variable: db_host in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Notice: Undefined variable: db_user in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Notice: Undefined variable:
db_password in C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Warning: mysql_connect()
[function.mysql-connect]: [2002] A
connection attempt failed because the
connected party did not (trying to
connect via tcp://localhost:3306) in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Warning: mysql_connect()
[function.mysql-connect]: A connection
attempt failed because the connected
party did not properly respond after a
period of time, or established
connection failed because connected
host has failed to respond. in
C:\Program Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
Fatal error: Maximum execution time of
30 seconds exceeded in C:\Program
Files
(x86)\EasyPHP-5.3.2\www\prtcl\index.php on line 29
as you can see I'm using EasyPHP and, since this very code used to work before (with a different db, while using manually configured apache/mysql), may be that the reason? Other infos: I made the db using phpmyadmin and I have win7
thank you
Don't ever take your db credentials from $_POST without validation. This is such a terrible idea and this is what's causing your errors, as these keys are not being defined in $_POST, but they could be and the results could be disastrous!
try putting this in connect.php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "prtcl";
$db = mysql_connect($db_host, $db_user, $db_password);
mysql_select_db($db_name, $db);
if(!$db) {
die('Could not connect: ' . mysql_error());
}
then in your other pages use:
require_once("path/to/connect.php");
// ... whatever else you do on this page...
I would find out why the $db_host variable is not getting defined. That might be a problem with your form input. Check to see that the name of that input field really is 'localhost'. Also, as Mark Baker mentioned above, you really want to do some input cleansing to guard against sql injection attacks. Plus, is there any reason you can't hardcode the db host info into a configuration file?

Categories