2
votes

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

5 Answers

3
votes

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);
             ^ 
1
votes

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
votes

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

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
0
votes

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