Php PDO connection issue - php

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

Related

XAMPP: Only one usage of each socket addres

I have created a local php server using XAMPP, for my app to consume.
My app makes a request every second to a php file to check if there is something to do. I've noticed that if I run multiple instantnces of my app, sometimes it starts returning this error:
Warning: mysqli::__construct(): (HY000/2002): Only one
usage of each socket address (protocol/network address/port) is
normally permitted. in C:\xampp\htdocs\testProject\common.php
on line 2 Warning: mysqli_query():
Couldn't fetch mysqli in
C:\xampp\htdocs\testProject\signal_update.php on line
2 Warning: mysqli_error(): Couldn't fetch
mysqli in C:\xampp\htdocs\testProject
The code to connect to the BD is the following:
function openCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "test_database";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
How can I fix this?

Connection failed mysqli 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

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 db connection issue with exception [duplicate]

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

MySQL won't load on localhost

I can't get mysql table to load on localhost. below i used PDO and the table worked fine. no errors...the table works fine in phpmyAdmin...please help driving me nuts. i imported table from my web server to local host from a earlier version..
<?php
$user = "root";
$pass = "root";
try {
$dbh = new PDO('mysql:host=localhost;dbname=iosLeads',$user,$pass);
foreach($dbh->query('SELECT * from Customer') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error! :" . $e->getMessage()."<br/>";
die();
}
?>
but when i used this code it show a blank screen, no errors..i tried same code with other tables and it works fine.
<?php
// set up the connection variables
$db_name = 'iosLeads';
$hostname = 'localhost';
$username = 'root';
$password = 'root';
// connect to the database
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
// a query get all the records from the users table
$sql = 'SELECT * FROM Customer';
// use prepared statements, even if not strictly required is good practice
$stmt = $dbh->prepare( $sql );
// execute the query
$stmt->execute();
// fetch the results into an array
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
// convert to json
$json = json_encode( $result );
// echo the json string
echo $json;
?>
I'm getting this in my php error.log
[17-Dec-2014 03:39:55 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
[17-Dec-2014 03:40:00 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
[17-Dec-2014 03:40:23 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8888' (20)' in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php:10
Stack trace:
0 /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php(10): PDO->__construct('mysql:host=loca...', 'root', 'root')
1 {main}
thrown in /Applications/MAMP/htdocs/iosLeads/iosCustomerError.php on line 10
If the array in $result has strings other than UTF-8 encoded, the function json_encode() may fail and will return false (see PHP manual json_encode). To check this, dump the value of $result before using json_encode():
var_dump($result);
If you have an array in $result but no other return value than false from json_encode(), you must set the correct character encoding for the database connection.

Categories