4

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';
?>
Community
  • 1
  • 1
sang
  • 375
  • 2
  • 8
  • 23

5 Answers5

3

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);
             ^ 
pavel
  • 26,538
  • 10
  • 45
  • 61
1

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);
}
0

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);
Community
  • 1
  • 1
SpongePablo
  • 870
  • 10
  • 24
0

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
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

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

  1. NAME of Constant must be CAPITAL
  2. use of mysql instead of mysqli
  3. the order of parameter must be as

    • host
    • db user
    • db user pass
    • db name
Awais Tahir
  • 180
  • 1
  • 10