PHP db connection issue with exception [duplicate] - php

This question already has an answer here:
why this warning came ::::::mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known [closed]
(1 answer)
Closed 8 years ago.
so this is my code:
<?php
class DB{
private $host = 'loscalhost';
private $user = 'root';
private $password = '';
function __construct(){
try {
$connect_db = mysql_connect($this->host, $this->user, $this->password);
}catch(Exception $e){
echo 'DB connection failed: ', var_dump($e), "\n";
}
}
}
I changed the host variable value so i can display the message DB connection failed..., but my question is why is it not displayed? instead i get this: Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known

Host name is localhost but you used loscalhost
private $host = 'loscalhost';
should be
private $host = 'localhost';

Related

Connect PHP to SQL Server [duplicate]

This question already has answers here:
How to use PHP to connect to sql server
(17 answers)
Using PHP connect Microsoft SQL Server 2014
(1 answer)
PHP script to connect to SQL Server
(1 answer)
Connect to SQL Server using PHP?
(1 answer)
Closed 27 days ago.
I'm bulding an Intranet for my workplace and I need to connect it to a Database. I already have a script that connect my website throught XAMPP on PHPmyadmin, but now my boss asked me to connect the website to a database we have on Microsoft SQL server Management program and maybe I'm stupid but I'm not understanding how to do it and what's the difference.
And also: to connect to Microsoft SQL i use windows authentication so I dont know what to write on the php script.
This is my connect.php script I have and works on PHPMYADMIN:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "intranet";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
And this is the one I wrote for the database I have on Microsoft SQL (I change the server cause it's the one I use to connect when I open the program):
<?php
$servername = "EDP004\SQLEXPRESS";
$username = "root";
$password = "";
$dbname = "INTRANET_DD";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
And this is the error I get on browser:
Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo for EDP004\SQLEXPRESS failed: unknown Host. in C:\xampp\htdocs\log\material\center\html\scripts\config.php on line 8
Fatal error: Uncaught mysqli_sql_exception: php_network_getaddresses: getaddrinfo for EDP004\SQLEXPRESS failed: unknown Host. in C:\xampp\htdocs\log\material\center\html\scripts\config.php:8 Stack trace: #0 C:\xampp\htdocs\log\material\center\html\scripts\config.php(8): mysqli->__construct('EDP004\\SQLEXPRE...', '', '', 'INTRANET_DD') #1 C:\xampp\htdocs\log\material\center\html\index.php(2): require_once('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\log\material\center\html\scripts\config.php on line 8

PHP, MySQL returns "Connection failed: The server requested authentication method unknown to the client" [duplicate]

This question already has answers here:
PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client [duplicate]
(8 answers)
Closed 2 years ago.
Trying to connect to local Mysqli DB.
Database Connection:
<?php $con= new mysqli("localhost","Kobe24","Kobei987","Bkn_Data");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
Returns this:
Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in C:\Apache24\htdocs\poc\practice_project\database_connection.php on line 1
Connection failed: The server requested authentication method unknown to the client
Any feedback would help. Been researching this problem but no definitive solutions.
Can you try this mysqli_connect() functions as per code below.
Please also ensure that database user and password are correct and that the user has permission to connect to the database
<?php
$dbhost = 'localhost:3306';
$dbuser = 'enter database user';
$dbpass = 'enter password';
$con = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $con ){
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($con);
?>

getaddrinfo failed: No such host is known [duplicate]

This question already has answers here:
Message: mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known
(2 answers)
Closed 1 year ago.
I tried to connect to phpMyAdmin database but encounter an error Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known. Below coding is the one that I currently use.
$servername = "http://localhost:8080";
$username = "root";
$password = "";
$db = "dbLogin";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: ".$conn->connect_error);
}
else {
echo "Connection success!";
}
When I run my system, It will still show the message connection success! but together with the error I mentioned.
replace server name from
$servername = "http://localhost:8080";
to
$servername = "localhost";

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

I can not connect with PHP PDO

In my PHP apicación I connect to a MySQL database with PDO, but I'm having the following problem, the error message;
Warning: PDO :: __construct () [pdo. - Construct]: [2002]
"No connection Could be made ​​Actively Because the target machine refused it."
(trying to connect via tcp :/ / localhost: 3306)
Fatal error: Uncaught exception 'Exception' with message 'Connection failed: SQLSTATE [HY000] [2002]
The code to access the database is:
private $db = NULL;
const DB_SERVER = "localhost";
const DB_USER = "root";
const DB_PASSWORD = "usbw";
const DB_NAME = "status_poster";
public function __construct() {
//**below the error here!!**
$dsn = 'mysql:dbname=' . self::DB_NAME . ';host=' . self::DB_SERVER;//
//var_dump($dsn);
try {
$this->db = new PDO($dsn, self::DB_USER, self::DB_PASSWORD);
} catch (PDOException $e) {
throw new Exception('Connection failed: ' . $e->getMessage());
}
return $this->db;
}
What I find strange is that the same parameters to connect to "mysqli" I have no problems and I connect successfully, run CRUD statements and no problems, the code is as follows;
The code
function getProducts() {
$products = array();
$mysqli = new mysqli("localhost", "root", "usbw", "Datos");
if (mysqli_connect_errno()) {
printf("Error in conection: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Id,sku,name,price FROM products";
if ($result = $mysqli->query($query)) {
while ($obj = mysqli_fetch_object($result)) {
//instructions...
}
}
$mysqli->close();
return $products;
}
Well, I need to connect with PDO but I can not find the solution,
waiting for suggestions or help!
Sorry, can not find in the forum how to respond with a picture, but is this;
solve my problem, change the port from 3307-3306 (3306 was the port that went out in error) of USBWebserver. now work!
note: above the port was 3307
solve my problem, change the port from 3307-3306 (3306 was the port that went out in error) of USBWebserver. now work!

Categories