Reputation: 405
I'm trying to connect to a localhost database using php.
but it's shown
Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp2\htdocs\wikifiesto\wf-insertcase.php on line 7
Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp2\htdocs\wikifiesto\wf-insertcase.php on line 7 error connecting to database
the structure of server, user, pass is localhost, pma, ' '
here's my code with the name 'wf-insertcase.php':
<?php
define('dbuser', 'pma');
define('dbpass', '');
define('dbserver', 'localhost');
define('dbname', 'wikifiesto');
$conn = mysql_connect(dbuser, dbpass, dbserver, dbname);
if (!$conn) {
die('error connecting to database');
}
echo 'you have created case';
?>
Upvotes: 4
Views: 70098
Reputation: 11
Warning "No such host is known" occur because "Host" is not set properly.
Please replace the following lines of your code
$conn = mysql_connect(dbuser, dbpass, dbserver, dbname);
if (!$conn) {
die('error connecting to database');
}
with
$conn = mysql_connect(dbserver,dbuser,dbpass);
if (!$conn){
die('error connecting to database');
}else{
mysql_select_db(dbname, $conn);
}
Upvotes: 1
Reputation: 178
use mysqli instead of mysql has deprecated
<?php
define('DBUSER', 'pma');
define('DBPASS', '');
define('DBSERVER', 'localhost');
define('DBNAME', 'wikifiesto');
$conn = new mysqli(DBSERVER, DBUSER, DBPASS, DBNAME);
if (!$conn) {
die('error connecting to database');
}
echo 'you have created case';
?>
you have some following non-standard things/errors
the order of parameter must be as
Upvotes: 0
Reputation: 27082
You have bad params order in your function. DB server has to be the first param.
$conn = mysql_connect(dbserver, dbuser, dbpass);
DB name isn't allowed there, use mysql_select_db(dbname)
.
All mysql_* are deprecated, see http://php.net/manual/en/function.mysql-connect.php and MySQLi
extension.
$conn = mysqli_connect(dbserver, dbuser, dbpass, dbname);
^
Upvotes: 3
Reputation: 38584
In Mysql
<?php
$dbuser = 'pma';
$dbpass = '';
$dbserver = 'localhost';
$dbname = 'wikifiesto';
$conn = mysql_connect($dbserver, $dbuser, $dbpass);//Connecting to localhost
$db = mysql_select_db($dbname, $conn);//connecting database
In mysqli
$conn = mysqli_connect($dbserver,$dbuser,$dbpass,$dbname)//MySQLi Procedural
Upvotes: 0
Reputation: 890
It will be broken soon, as those functions are deprecated and will be removed soon from PHP.
If you still want to use them, use the right order for mysql_connect
$mysql_handler = mysql_connect($host, $user, $pass);
mysql_select_db($database_name, $mysql_handler);
Upvotes: 0