Connection failed mysqli php - php

I have set a connection to a mysql server (OVH) in PHP like this :
$con = mysqli_connect("myserver.mysql.db","user","pass","bdd") or die("Couldn't connect");
And it can't connect it, saying the host in unknow. But this is the only informations I have about the server
What can I do to get over this problem ?
Thanks
EDIT :
Here is php code :
class dbconnection{
function connect(){
$con = mysqli_connect("myserv.mysql.db","user","mdp","bdd") or die("Couldn't connect" . $con->connect_error);
return $con;
}
}
Then :
$con = new dbconnection();
$db = $con -> connect();
And here is the error message :
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: unknown host. in C:\xampp\htdocs\hackaton\class\dbconnection.php on line 11
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: unknown host. in C:\xampp\htdocs\hackaton\class\dbconnection.php on line 11
Notice: Trying to get property of non-object in C:\xampp\htdocs\hackaton\class\dbconnection.php on line 11
Couldn't connect

Related

(HY000/2002): Operation timed out in macOS

I am learning to connect to sql databases using php:
https://www.youtube.com/playlist?list=PLF8OvnCBlEY1bFcTW9JKKO7WJn_Bf9LZ1
But the connection is not made, and the error code is:
Warning: mysqli_connect(): (HY000/2002): Operation timed out in /Users/Ahmead/Sites/PHPTest/myTutorials/dbconnect.php on line 12
cannot connect to database fileld:Operation timed out
myCode php:
<?php
$host="172.0.0.1";
$user="root";
$password="";
$database="admins";
$connect= mysqli_connect($host, $user, $password, $database);
if(mysqli_connect_errno()){
die("cannot connect to database fileld:". mysqli_connect_error());
}
else {
echo 'Database is connected';
}
?>

Is there a way to find servername, username and password of an MySQL server if I can log in via phpmyadmin?

Imagine I can access a MariaDB Database via phpmyadmin, e.g.
https://test.com/phpMyAdmin
username: some_username
password: some_password
Now I just want to use php to search whether some value exists; However, to do so I need to connect to the database with a servername, username and password;
I tried the servername
https://test.com`
with username and password as above but no luck; Is there a way to find that out?
Right now my currect code looks like that:
<?php
$servername = "https://test.com";
$username = "some_username";
$password = "some_password";
$conn = new mysqli($servername, $username, $password, "DB1");
// Überprüfe Verbindung
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
which gives me the error
Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\test.php on line 9
Warning: mysqli::__construct(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\test.php on line 9
Connection failed: php_network_getaddresses: getaddrinfo failed: No such host is known.

PHP Mysqli fail to getaddress

I get the following error when I try to establish a connection to my database using mysqli:
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo
failed: No such host is known. in
C:\xampp\htdocs\emarps\database\getuserdetails.php on line 4
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known.
in C:\xampp\htdocs\emarps\database\getuserdetails.php on line 4
Connect failed: php_network_getaddresses: getaddrinfo failed: No such
host is known.
Below is my php code for connecting to the database:
$host_name = "127.0.0.1";
$database = "db565263480";
$user_name = "dbo565263480";
$password = "emarps2015!";
$link = mysqli_connect('localhost', $user_name, $password, $database);
// check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
Please advise what am I doing wrong?

i have face this error Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known.

my error is
`
if($_SERVER['HTTP_HOST']=="localhost" || $_SERVER['HTTP_HOST']=="server" )
{
// Config setting for localhost.
define("DBSERVER","localhost");
define("DBNAME","raj12");
define("DBUSER","root");
define("DBPASS","");
$connect= mysql_connect("DBSERVER","DBUSER","DBPASS") or die(mysql_error()."cannot connect local");
}
else
{ // Config setting for live server.
define(DBSERVER,"localhost");
define(DBNAME,"raj12");
define(DBUSER,"root#123");
define(DBPASS,"root#123");
mysql_connect(DBSERVER,DBUSER,DBPASS) or die("cannot connect server");
}
mysql_select_db(DBNAME)or die("error in selecting database");
include("db.class.php");
$obj=new DB(DBNAME,DBSERVER,DBUSER,DBPASS);
?>`
Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\rajsons\lib\config.php on line 14##
You are passing string literals instead of the constants you defined.
mysql_connect("DBSERVER","DBUSER","DBPASS")
Remove the quotes.
change to this:
if($_SERVER['HTTP_HOST']=="localhost" || $_SERVER['HTTP_HOST']=="server" )
{
// Config setting for localhost.
define(DBSERVER, "localhost");
define(DBNAME, "raj12");
define(DBUSER, "root");
define(DBPASS, "");
$connect = mysql_connect(DBSERVER, DBUSER, DBPASS) or die(mysql_error() . "cannot connect local");
}
And better to use mysqli instead of mysql

Php PDO connection issue

I am using the following php code to connect to a remote database:
<?php
error_reporting(E_ALL);
ini_set( 'display_errors','1');
$db_host = "host";
$db_user = "user";
$db_pass = "pass";
$db_name = "name";
try{
$dbc = new PDO("mysql:dbname=$db_name;host=$db_host",$db_user,$db_pass);
}
catch(PDOException $e){
echo '<p>There was a problem with your connection: '.$e->getMessage().'</p>';
}
$query = "some query";
$results = $dbc->query($query);
$row = $results->rowCount();
?>
But it keeps generating the following error message:
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /example.php on line 11
There was a problem with your connection: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
//shouldn't this error below not even show because the try never completes?
Notice: Undefined variable: dbc in /example.php on line 25
Fatal error: Call to a member function query() on a non-object in /example.php on line 25

Categories