Cannot connect to mysql database on my website - php

I have a website that I should put on my domain.I have encountered an issue when I tried connecting to MySQL database so my code is not working. When I put
define("DB_HOST","http://domain");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","dbName");
$connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if(!$connection){
die();
}
mysqli_set_charset($connection,"utf8");
I get this error in log
Failed to connect to MySQL: Plugin http could not be loaded: /usr/lib64/mysql/plugin/http.so: cannot open shared object file: No such file or directory
otherwise, I tried to remove that HTTP and just use the domain as DB_HOST, but then I get error
define("DB_HOST","mydomain.com");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","dbName");
$connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
if(!$connection){
die();
}
mysqli_set_charset($connection,"utf8");
PHP Warning: mysqli_connect(): (HY000/2002): Can't connect to MySQL server on 'domain.com' (115) in /home3/domain/public_html/a/i.php on line 7
Can anyone please help me find the solution to make this work?

Here's how I define a MYSQLI connection;
$con = mysqli_connect("localhost","username","password","database");
When you want to run an operation you just invoke the $con variable.
If you have issues check the permissions for the MYSQL user, they should at the very least include;
delete
insert
select
show view
update

Related

Mysql Operation timeout Error

Warning: mysqli_connect(): (HY000/2002): Operation timed out in
/Applications/XAMPP/xamppfiles/htdocs/conn.php on line 4
Connection failed: Operation timed out
Get the following error while connecting to XAMPP on Mac OS X.
Code line is
$conn=mysqli_connect("SNA","root","","localhost");
// Check connection
if (mysqli_connect_errno()) {
echo "Connection failed: " . mysqli_connect_error();
}
Is it a Mysql Host problem or a Mysql default socket problem and how to resolve it? Default Host is 3306. I dont know where my mysql.sock file is generated and whether its generating or not.
change your code here, interchange host and database name
$conn=mysqli_connect("localhost","root","","SNA");
check php manual for more info : http://php.net/manual/en/function.mysqli-connect.php

I get connection error when I add my database name in connection

Warning: mysqli_connect(): (42000/1044): Access denied for user '-----'#'localhost' to database '----' in ---.php on line 4
Unable to connect to MySQL
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die("Unable to connect to MySQL");
This is how I connect. If I remove DB_NAME from there, I don't get an error and connect database but whenever I add DB_NAME, I get this error. DB_NAME is definitely correct but I don't see why? By the way for DB_HOST, I use localhost.
---Solved---
I finally solved the problem. So, for those of you who have the same problem, I am gonna explain everything. Actually, I decided to use table name before my query and than mysqli gave me another error saying something like "INSERT command denied to user ..." So, I opened up phpMyAdmin and changed my DB_USER to the name that is written in Database Server section in phpMyAdmin. You will see something like this;
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.5.42-cll - MySQL Community Server (GPL)
Protocol version: 10
User: -HERE-
Server charset: UTF-8 Unicode (utf8)
So you need to use use the username, which is written in User part. After I changed my DB_USER I was able to add DB_NAME. Basically all the problems I was having (not being able to use DB_NAME and not being able to use sql commands) caused my permission.
The key part is; I was using DB_NAME, which I used to create database in my server and it also let me connect to the database but As I can see it is not the root. So, if you are having "denied" error, make sure that you check the values you typed in mysqli_connect!
You can use
# mysqli_connect(hostname, username, password, database)
or
#mysqli_connect(hostname,username, password)
# mysqli_select_db(true, database);
or
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed : mysqli_connect_error();
}
mysqli_select_db($con,"db");
if you are using localhost then try this
<?php
$con = mysqli_connect("localhost", "root", "", "YOUR_DB_NAME");
if (mysqli_connect_error()) {
die ("Could not Connect");
}
?>
and if you are doing this online then replace localhost with host_name root with username "" with password and at last your DB_Name. I hope this would solve your problem :)

Why php PDO uses a different from hostname to the one used by mysql_connect() when connecting to a remotely hosted mysql database?

I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.

Having trouble with connecting to MySQL database in php

I've made mysql.php file which contain codes to connect to my database.
However, i'm getting this error message over and over again.
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 7
Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to MySQL server on 'localhost' (10061) in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 8
Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 8
Unable to select database
I've checked if my username, host, database, and password were misspelled, but they were all correct.
I've looked around the internet, but couldn't seem to find the right one for my problem.
My mysql.php file contains:
<?php
// Mysql settings
$user = "example_example";
$password = "example";
$database = "example_example";
$host = "localhost";
mysql_connect($host,$user,$password);
mysql_select_db($database) or die( "Unable to select database");
?>
I've changed my user, password and host name to "example".
I just couldn't find the solution for myself, and I need your kind help.
Thank you in advance.
Please try to find out the error by using mysql_error() function as follows :
mysql_connect($host,$user,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
and then try to find the exact reason for the failure.
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061)
here a list of questions you have to do when this error appears:
1.- is mysql running on the server?
1.1.- is mysql running on default port or in a custom port?
1.2 - is mysql accepting connexions (from localhost, from an ip...?
1.3 - your firewall is blocking mysql?
2.- the database exists on the server?
3.- the user:password have access to that database?
3.1- the user:password can log in from localhost, from an ip... from any place that mysql
is accepting?
I think I don't forget anything
Please check first that your mysql server is running properly.
Mysql settings
$user = "example_example";
$password = "example";
$database = "example_example";
$host = "localhost";
$cn=mysql_connect($host,$user,$password);
mysql_select_db($database,$cn);
Please try this it may work
I am not expert of php but this code is working perfectly in my website
if you are getting any error after applying this code then please inform me i will rty to solve the problem.
Thanks
You should provide the connection that need to use from database whle selecting database. Also use single quote instead of double quotes.
<?php
// Mysql settings
$user = 'example_example';
$password = 'example';
$database = 'example_example';
$host = 'localhost';
$link = mysql_connect($host,$user,$password);
mysql_select_db($database,$link) or die( "Unable to select database");
?>
Reference
You have to get the database information first. You posted this problem when you try to connect to your database in your website hosting, right?
So, go to your hosting control panel to get the MySQL database (and take note of where the database is stored on! Sometimes some hosting providers don't use "localhost". Instead, some of them may use 'serverXX.yourhostingprovider.tld' where XX means the server ID), and then change the corresponding variables with the value you've obtained from the hosting control panel.

Cant connect to mysql: MySQL server has gone away

I'm start to learn mysql not so long time ago, and now i have problem, that i cant solve by myself.
I'm trying to connect to mysql server with php. My admin gaved me ip, port, login, pass and db name.
Php file looks like this:
$hostname = '192.168.1.20:1433';
$username = "h";
$password = "h";
$dbName = "mydb";
mysql_connect($hostname,$username,$password) OR DIE(mysql_error());
mysql_select_db($dbName) or die(mysql_error());
$query = "SELECT * FROM `insy` WHERE 1;";
$result=mysql_query($query) or die(mysql_error());
echo "works!";
mysql_close();
When i run page, its do nothing few minutes, than errors appears:
Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in N:\home\192.168.1.19\www\phlib.php on line 12
Warning: mysql_connect() [function.mysql-connect]: Error while reading greeting packet. PID=1908 in N:\home\192.168.1.19\www\phlib.php on line 12
Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in N:\home\192.168.1.19\www\phlib.php on line 12
MySQL server has gone away
12'th line is:
mysql_connect($hostname,$username,$password) OR DIE(mysql_error());
I tried to create ODBC Sql Server via Control Panel -> Administration - with same parameters - it works, connection is ok, and i ca see the Db's.
Tell me please, where is my fault?
I hope, i was clear.
Thanks for help.
you can try mysql_ping($conn) if your server connection lost.
http://php.net/manual/en/function.mysql-ping.php
I've just solved that problem.. the thing was when I did create a new user in MySql, after writing the user, the server ask you for where will be the "server" and you put localhost:8080.. ERROR! don't put the port!! just localhost.
And thats how I solved it..

Categories